unix时间戳的公式是什么?

发布于 2024-12-05 11:49:16 字数 614 浏览 1 评论 0原文

首先,我知道这个问题已经在这里被问/排序了:以数学方式从 unix 时间戳计算天数? .

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

eg. 1316278442 = 2011-09-17

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

我已经在这呆了一整天了!我唯一能得到的是星期几。

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

速度是问题,这就是为什么我不想使用 date('Ymd',$timestamp);

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

预先感谢您的帮助。

First of all, i know this question has been sort of asked/sort-of answered here: Calculate day number from an unix-timestamp in a math way? .

I need a custom function/formula for this. so it only returns a ISO format date. "YYYY-MM-DD".

eg. 1316278442 = 2011-09-17

EDIT by Ext!
THIS IS WRONG ! Please don't read this.

I've been at this all day! The only thing i managed to get out is the day of the week.

$dayOfWeek=($timestamp/86400)%7; //And here 1 is Saturday, 7 is Friday

Speed is the issue, that is why i don't want to use date('Y-m-d',$timestamp);

If you cannot help me whit a custom function or formula, at least give me a better explanation on how to do this. It was done in so many languages, there must be someone out there that knows how to do this.

Thank you in advance for your help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

嗳卜坏 2024-12-12 11:49:16

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

<一个href="https://github.com/php/php-src/blob/d57eefe6227081001978c3a63224065af8b5728e/ext/date/lib/unixtime2tm.c#L39" rel="nofollow">https://github.com/php/php-src/blob/d57eefe6227081001978c3a63224065af8b5728e/ext/date/lib/unixtime2tm.c#L39

如您所见,这有点复杂闰年等

——

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

Here is the function that date() and DateTime::setTimestamp() use to compute the date from a unix timestamp:

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

As you can see, this is a bit complicated by leap years, etc.

--

That said, if you need only the day of the week, it seems that you can safely ignore leap years, and just use the formula you given in the question: $dayOfWeek=($timestamp/86400)%7

情深已缘浅 2024-12-12 11:49:16

好的。功能完成。它采用 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);
?>

Ok. The function is complete. It takes a unix timestamp and returns a YYYY-MM-DD. This was all i needed. I hope it helps anyone ...

<?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);
?>
盗心人 2024-12-12 11:49:16

您可以先使用 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";

You could convert to julian first with unixtojd() and then use cal_from_jd to split into year,month,day.
It's a little faster. The code below gives me this result:

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";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文