从星期日开始获取一周的问题 [英] Issue with getting week starting from sunday

查看:33
本文介绍了从星期日开始获取一周的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我创建的用于将星期日作为一周的开始日的函数,

<预><代码>函数 getCurrentIntervalOfWeek($liveratetime) {//获取每周的开始.$dayofweek = date('w', $liveratetime);$getdate = date('Y-m-d', $liveratetime);$createstart = strtotime('last Sunday', $getdate);$weekstart = ($dayofweek == 0) ?$liveratetime : $createstart;//获取一周的当前时间间隔,即星期日 00:00:00 UTC$currentInterval = mktime(0,0,0, date('m', $weekstart), date('d', $weekstart), date('Y', $weekstart));返回 $currentInterval;}

这里的liveratetime 是一周中任何一天的纪元时间.基本上,此函数采用 liveratetime 并查找上一个星期日,以便获得该 perticulat liveratetime epoch 的当前间隔.

但这里的问题是,每当我尝试从中获取当前间隔时,

$createstart = strtotime('last Sunday', $getdate);

给我

-345600

.我不明白为什么?任何人都可以分享这方面的信息.

这通常发生在过去的日期,例如

2007-10-02

解决方案

Strtotime 的第二个参数是 TIMESTAMP,而不是日期的字符串表示.试试:

$createstart = strtotime('last Sunday', $liveratetime);

它为您提供 -345600,因为当 $getdate(即 Y-m-d)被解析为 int 时,结果为 0 - 纪元时间.所以,结果是纪元时间的最后一个星期天......

Following is the function which I have created for getting sunday as a starting day of a week,


function getCurrentIntervalOfWeek($liveratetime) {
    // get start of each week.
    $dayofweek = date('w', $liveratetime);
    $getdate = date('Y-m-d', $liveratetime);
    $createstart = strtotime('last Sunday', $getdate);
    $weekstart = ($dayofweek == 0) ? $liveratetime : $createstart;
    // get the current time interval for a week, i.e. Sunday 00:00:00 UTC
    $currentInterval = mktime(0,0,0, date('m', $weekstart), date('d', $weekstart), date('Y', $weekstart));
    return $currentInterval;
}

Here liveratetime is the epoch time of any day in a week. Basically this function takes the liveratetime and looks for last sunday so as to get the current interval for that perticulat liveratetime epoch.

but the issue here is that, whenever I try to get current interval from this for certain liveratetime the

$createstart = strtotime('last Sunday', $getdate);

gives me

-345600

. And I am not getting why? can anyone please share a light on this.

This usually happens on the past dates like

2007-10-02

解决方案

Strtotime's second parameter is a TIMESTAMP, not a string representation of a date. Try:

$createstart = strtotime('last Sunday', $liveratetime);

It gives you -345600, because when $getdate, which is Y-m-d, is parsed as an int results in 0 - the epoch time. So, last sunday from epoch time is the result...

这篇关于从星期日开始获取一周的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆