【问题标题】:What is the unix timestamp formula?什么是unix时间戳公式?
【发布时间】:2011-11-19 08:10:53
【问题描述】:

首先,我知道这个问题已经在这里被问过/回答:Calculate day number from an unix-timestamp in a math way?

我需要一个自定义函数/公式。所以它只返回一个 ISO 格式的日期。 “YYYY-MM-DD”。

eg. 1316278442 = 2011-09-17

由分机编辑! 这是错误的!请不要阅读本文。

我整天都在做这个!我唯一设法摆脱的就是星期几。

$dayOfWeek=($timestamp/86400)%7; //这里1是星期六,7是星期五

速度是问题,这就是我不想使用date('Y-m-d',$timestamp);的原因

如果你不能帮助我使用自定义函数或公式,至少给我一个更好的解释来说明如何做到这一点。它以多种语言完成,一定有人知道如何做到这一点。

提前感谢您的帮助。

【问题讨论】:

  • 是什么让您认为date('Y-m-d',$timestamp) 会比自己计算日期慢?
  • 如果 date 速度不够快,您可以考虑使用其他语言
  • 人们是否可以忽略“速度”问题并回答有关 unix 时间戳的问题。我很高兴有人问过这个问题,我也需要知道。所以人们请...
  • @Ext,你的两个代码做的不一样,你的计算不准确。这可以解释这一点;)

标签: php datetime timestamp unix-timestamp


【解决方案1】:

这是date()DateTime::setTimestamp() 用于根据unix 时间戳计算日期的函数:

https://github.com/php/php-src/blob/d57eefe6227081001978c3a63224065af8b5728e/ext/date/lib/unixtime2tm.c#L39

如您所见,闰年等情况有点复杂。

--

也就是说,如果您只需要星期几,您似乎可以放心地忽略闰年,只需使用您在问题中给出的公式:$dayOfWeek=($timestamp/86400)%7

【讨论】:

  • 至少没有闰秒!
  • 感谢您的回答(尽管它是一个参考),而不是像其他人一样胡扯问题中的错误!
  • @Nicholas 显然您在使用 unix 时间戳时不需要关心闰秒 (derickrethans.nl/leap-seconds-and-what-to-do-with-them.html)
  • 是的,我就是这么说的:你必须担心闰年,但对于 unix 时间戳,至少没有闰秒。
  • @NicholasWilson 由于地球自转的变化,存在闰秒:en.wikipedia.org/wiki/Leap_second 最近的一个是在 2012 年 6 月 30 日添加的
【解决方案2】:

好的。功能齐全。它需要一个 unix 时间戳并返回一个 YYYY-MM-DD。这就是我所需要的。我希望它可以帮助任何人......

<?php
$t=1325522004;//return 2011-09-19
/*
 * Transform a Unix Timestamp to ISO 8601 Date format YYYY-MM-DD 
 * @param unix timestamp
 * @return Returns a formated date (YYYY-MM-DD) or false
 */
function unixToIso8601($timestamp){
    if($timestamp<0){return false;}//Do not accept negative values
    /* Too many constants, add this to a class to speed things up. */
    $year=1970;//Unix Epoc begins 1970-01-01
    $dayInSeconds=86400;//60secs*60mins*24hours
    $daysInYear=365;//Non Leap Year
    $daysInLYear=$daysInYear+1;//Leap year
    $days=(int)($timestamp/$dayInSeconds);//Days passed since UNIX Epoc
    $tmpDays=$days+1;//If passed (timestamp < $dayInSeconds), it will return 0, so add 1
    $monthsInDays=array();//Months will be in here ***Taken from the PHP source code***
    $month=11;//This will be the returned MONTH NUMBER.
    $day;//This will be the returned day number. 

    while($tmpDays>=$daysInYear){//Start adding years to 1970
        $year++;
        if(isLeap($year)){
            $tmpDays-=$daysInLYear;
        }
        else{
            $tmpDays-=$daysInYear;
        }
    }

    if(isLeap($year)){//The year is a leap year
        $tmpDays--;//Remove the extra day
        $monthsInDays=array(-1,30,59,90,120,151,181,212,243,273,304,334);
    }
    else{
        $monthsInDays=array(0,31,59,90,120,151,181,212,243,273,304,334);
    }

    while($month>0){
        if($tmpDays>$monthsInDays[$month]){
            break;//$month+1 is now the month number.
        }
        $month--;
    }
    $day=$tmpDays-$monthsInDays[$month];//Setup the date
    $month++;//Increment by one to give the accurate month

    return $year.'-'.(($month<10)?'0'.$month:$month).'-'.(($day<10)?'0'.$day:$day);
}
function isLeap($y){
    return (($y)%4==0&&(($y)%100!=0||($y)%400==0));
}
echo unixToIso8601($t);
?>

【讨论】:

    【解决方案3】:

    您可以先使用 unixtojd() 转换为 julian,然后使用 cal_from_jd 拆分为年、月、日。 它有点快。下面的代码给了我这个结果:

    2009-02-13 0.13018703460693 seconds  using date()
    2009-02-13 0.037487983703613 seconds using unixtojd(),cal_from_jd(),and sprintf()
    
    
    
    function microtime_float(){
        list($usec, $sec) = explode(" ", microtime());
        return ((float)$usec + (float)$sec);
    }
    $time_start = microtime_float();
    $unix_timestamp = 1234567890;
    for($i=0;$i<10000;$i++) {
        $d = date('Y-m-d',$unix_timestamp);
    }
    $time_stop = microtime_float();
    echo $d . " " . ($time_stop - $time_start) . " seconds using date()<br>\n";
    
    //////////////////////////
    
    
    $time_start = microtime_float();
    
    $unix_timestamp = 1234567890;
    for($i=0;$i<10000;$i++) {
        $julian_date = unixtojd($unix_timestamp);
        $date_array = cal_from_jd($julian_date, CAL_GREGORIAN);
        $d = sprintf('%d-%02d-%02d',$date_array['year'],$date_array['month'],$date_array['day']);
    }
    
    $time_stop = microtime_float();
    echo $d . " " . ($time_stop - $time_start) . " seconds using unixtojd(),cal_from_jd(),and sprintf()<br>\n";
    

    【讨论】:

      猜你喜欢
      • 2014-01-16
      • 2012-03-31
      • 1970-01-01
      • 2013-12-25
      • 2013-05-11
      • 2016-07-17
      • 2013-05-21
      • 2013-03-23
      相关资源
      最近更新 更多