在PHP中获取一周的第一天?
给定一个日期在PHP中获取一周的第一天?,php,datetime,Php,Datetime,给定一个日期MM dd yyyy格式,有人能帮我确定一周的第一天吗?试试这个: function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y') { $wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101')); $mon_ts = strtotime('-' . date('w', $wk_ts) + $first . '
MM dd yyyy
格式,有人能帮我确定一周的第一天吗?试试这个:
function week_start_date($wk_num, $yr, $first = 1, $format = 'F d, Y')
{
$wk_ts = strtotime('+' . $wk_num . ' weeks', strtotime($yr . '0101'));
$mon_ts = strtotime('-' . date('w', $wk_ts) + $first . ' days', $wk_ts);
return date($format, $mon_ts);
}
$sStartDate = week_start_date($week_number, $year);
$sEndDate = date('F d, Y', strtotime('+6 days', strtotime($sStartDate)));
(from)您使用解析日期并在结果上使用:
date('N', strptime('%m-%d-%g', $dateString));
这将为您提供给定日期本身的一周中的哪一天,其中0
=星期日和6
=星期六。从那里你可以简单地倒推到你想要的日期。这个怎么样
<?php
/* PHP 5.3.0 */
date_default_timezone_set('America/Denver'); //Set apprpriate timezone
$start_date = strtotime('2009-12-15'); //Set start date
//Today's date if $start_date is a Sunday, otherwise date of previous Sunday
$today_or_previous_sunday = mktime(0, 0, 0, date('m', $start_date), date('d', $start_date), date('Y', $start_date)) - ((date("w", $start_date) ==0) ? 0 : (86400 * date("w", $start_date)));
//prints 12-13-2009 (month-day-year)
echo date('m-d-Y', $today_or_previous_sunday);
?>
$first_day_of_week = date('m-d-Y', strtotime('Last Monday', time()));
$last_day_of_week = date('m-d-Y', strtotime('Next Sunday', time()));
替换时间()。当当前日期为星期日/星期一时,下星期日/上星期一方法将不起作用。这如何
$day_of_week = date('N', strtotime($string_date));
$week_first_day = date('Y-m-d', strtotime($string_date . " - " . ($day_of_week - 1) . " days"));
$week_last_day = date('Y-m-d', strtotime($string_date . " + " . (7 - $day_of_week) . " days"));
我曾经多次遇到过这个问题,并且总是惊讶于日期函数不能使这个问题变得更容易或更清晰。下面是我使用DateTime
类的PHP5解决方案:
/**
* @param DateTime $date A given date
* @param int $firstDay 0-6, Sun-Sat respectively
* @return DateTime
*/
function getFirstDayOfWeek(DateTime $date, $firstDay = 0) {
$offset = 7 - $firstDay;
$ret = clone $date;
$ret->modify(-(($date->format('w') + $offset) % 7) . 'days');
return $ret;
}
必须进行克隆以避免更改原始日期。保持简单:
<?php
$dateTime = new \DateTime('2020-04-01');
$monday = clone $dateTime->modify(('Sunday' == $dateTime->format('l')) ? 'Monday last week' : 'Monday this week');
$sunday = clone $dateTime->modify('Sunday this week');
?>
资料来源:
注意:正如一些用户评论的那样,$dateTime值将被修改。以下代码应适用于任何自定义日期,只需使用所需的日期格式即可
$custom_date = strtotime( date('d-m-Y', strtotime('31-07-2012')) );
$week_start = date('d-m-Y', strtotime('this week last monday', $custom_date));
$week_end = date('d-m-Y', strtotime('this week next sunday', $custom_date));
echo '<br>Start: '. $week_start;
echo '<br>End: '. $week_end;
在这里,我认为星期天是一周的第一天,星期六是一周的最后一天
$m = strtotime('06-08-2012');
$today = date('l', $m);
$custom_date = strtotime( date('d-m-Y', $m) );
if ($today == 'Sunday') {
$week_start = date("d-m-Y", $m);
} else {
$week_start = date('d-m-Y', strtotime('this week last sunday', $custom_date));
}
if ($today == 'Saturday') {
$week_end = date("d-m-Y", $m);
} else {
$week_end = date('d-m-Y', strtotime('this week next saturday', $custom_date));
}
echo '<br>Start: '. $week_start;
echo '<br>End: '. $week_end;
$m=strotime('06-08-2012');
$today=日期('l',$m);
$custom_date=strottime(日期($d-m-Y',$m));
如果($today=='Sunday'){
$week_start=日期(“d-m-Y”,百万美元);
}否则{
$week_start=date('d-m-Y',strottime('this week last sunday',$custom_date));
}
如果($today=='Saturday'){
$week_end=日期(“d-m-Y”,百万美元);
}否则{
$week_end=date('d-m-Y',strottime('this week next saturday',$custom_date));
}
回显“
开始:”$每周开始;
回显“
结束:”$周末;
输出:
开始日期:2012年8月5日
完:2012年8月11日这是我正在使用的
$day = date('w');
$week_start = date('m-d-Y', strtotime('-'.$day.' days'));
$week_end = date('m-d-Y', strtotime('+'.(6-$day).' days'));
$day包含一个从0到6的数字,表示一周中的某一天(星期日=0,星期一=1等)。
$week\u start包含当前星期日的日期,即mm dd yyyy。
$week\u end包含当前星期六的日期,即mm dd yyyy。编辑:以下链接不再在所述PHP版本上运行。它在PHP5.6上运行,这提高了strotime的可靠性,但并不完美!表中的结果是PHP 5.6的实时结果。
值得一提的是,以下是在确定一致的参考框架时,strotime的古怪行为的分解:
基本上,只有这些字符串才能可靠地为您提供相同的日期,无论您当前在一周中的哪一天调用它们:
strtotime("next monday");
strtotime("this sunday");
strtotime("last sunday");
那么:
$date = "2013-03-18";
$searchRow = date("d.m.Y", strtotime('last monday',strtotime($date." + 1 day")));
echo "for date " .$date ." we get this monday: " ;echo $searchRow; echo '<br>';
$date=“2013-03-18”;
$searchRow=日期(“d.m.Y”,标准时间(“上周一”),标准时间($date.+1天”);
回声“日期”。$date。我们得到这个星期一:”;echo$searchRow;回声“
”;
这不是最好的方法,但我进行了测试,如果我在本周,我会得到正确的星期一,如果我在星期一,我会得到正确的星期一。给定PHP 5.3之前的版本,以下函数将为您提供给定日期一周的第一天(在本例中为2013-02-03,星期日):
这个问题需要一个好的答案:-
输出:-
string '06-10-2013' (length=10)
这将处理年份边界和闰年。只需使用date($format,strotime($date,'LAST SUNDAY+1天')代码>另一种方法
$year = '2014';
$month = '02';
$day = '26';
$date = DateTime::createFromFormat('Y-m-d H:i:s', $year . '-' . $month . '-' . $day . '00:00:00');
$day = date('w', $date->getTimestamp());
// 0=Sunday 6=Saturday
if($day!=0){
$newdate = $date->getTimestamp() - $day * 86400; //86400 seconds in a day
// Look for DST change
if($old = date('I', $date->getTimestamp()) != $new = date('I', $newdate)){
if($old == 0){
$newdate -= 3600; //3600 seconds in an hour
} else {
$newdate += 3600;
}
}
$date->setTimestamp($newdate);
}
echo $date->format('D Y-m-d H:i:s');
这是我用来从任何日期获取一周的第一天和最后一天的方法。
在这种情况下,星期一是一周的第一天
$date = date('Y-m-d') // you can put any date you want
$nbDay = date('N', strtotime($date));
$monday = new DateTime($date);
$sunday = new DateTime($date);
$monday->modify('-'.($nbDay-1).' days');
$sunday->modify('+'.(7-$nbDay).' days');
你必须先得到下周一,然后得到下周一的“最后一个周一”。因此,如果给定的日期是星期一,它将返回与上周星期一不同的相同日期。使用非常简单:
这有点不同:
5.3.0-5.6.6的输出,php7@20140507-20150301,hhvm-3.3.1-3.5.1
4.3.5-5.2.17的输出
4.3.0-4.3.4的输出
在边缘情况下比较
像本周这样的相关描述有自己的背景。以下显示了本周星期一和星期日(如果是星期一或星期日)的输出:
$date = '2015-03-16'; // monday
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";
$date = '2015-03-22'; // sunday
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";
此外,它有点不同:
5.3.0-5.6.6的输出,php7@20140507-20150301,hhvm-3.3.1-3.5.1
4.3.5-5.0.5、5.2.0-5.2.17的输出
5.1.0-5.1.6的输出
4.3.0-4.3.4的输出
获取本周第一天(星期一)的最简单方法是:
strtotime("next Monday") - 604800;
其中604800-是一周内的秒数(60*60*24*7)
此代码将在下周一获取,并将其减少1周。这段代码在一周中的任何一天都能正常工作。即使今天是星期一。这是我找到的最短、最可读的解决方案:
<?php
$weekstart = strtotime('monday this week');
$weekstop = strtotime('sunday this week 23:59:59');
//echo date('d.m.Y H:i:s', $weekstart) .' - '. date('d.m.Y H:i:s', $weekstop);
?>
strotime
比new DateTime()->getTimestamp()
要快,因为我的时区是澳大利亚,而且strotime()
讨厌英国日期,所以我觉得这很令人沮丧
如果当前日期是星期日,则strotime(“本周星期一”)
将在第二天返回
要克服这一点:
注意事项:此项仅适用于澳大利亚/英国日期
如果希望星期一作为一周的开始,请执行以下操作:
$date = '2015-10-12';
$day = date('N', strtotime($date));
$week_start = date('Y-m-d', strtotime('-'.($day-1).' days', strtotime($date)));
$week_end = date('Y-m-d', strtotime('+'.(7-$day).' days', strtotime($date)));
假设星期一是一周的第一天,则此操作有效:
echo date("M-d-y", strtotime('last monday', strtotime('next week', time())));
一个聪明的方法是让PHP处理时区差异和夏令时(DST)。让我告诉你怎么做
此函数将生成从周一到周五的所有工作日,包括(方便生成工作日):
这将返回本周的datetimes周一到周五。要对任意日期执行相同操作,请将日期作为参数传递给DateTimeUtilities::getPeriodFromMondayUntilFriday
,从而:
foreach (DateTimeUtilities::getPeriodFromMondayUntilFriday('2017-01-02T15:05:21+00:00') as $day) {
print $day->format('c');
print PHP_EOL;
}
//prints
//2017-01-02T00:00:01+00:00
//2017-01-03T00:00:01+00:00
//2017-01-04T00:00:01+00:00
//2017-01-05T00:00:01+00:00
//2017-01-06T00:00:01+00:00
只对周一感兴趣,就像OP问的那样
$monday = DateTimeUtilities::getPeriodFromMondayUntilFriday('2017-01-02T15:05:21+00:00')->getStartDate()->format('c');
print $monday;
// prints
//2017-01-02T00:00:01+00:00
这是上周第一天和上周最后一天的一行代码,作为DateTime
对象
$firstDay = (new \DateTime())->modify(sprintf('-%d day', date('w') + 7))
->setTime(0, 0, 0);
$lastDay = (new \DateTime())->modify(sprintf('-%d day', date('w') + 1))
->setTime(23, 59, 59);
你说的一周的第一天是什么意思?你是说太阳吗,蒙?或者你的意思是一周的第一天?一周的第一天是固定的:是星期一(或者可能是星期天)。你的意思是你想得到给定任意日期之前的星期一的日期吗?什么
2015-03-16
2015-03-22
2015-03-23
2015-03-22
2015-03-30
2015-03-29
$date = '2015-03-16'; // monday
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";
$date = '2015-03-22'; // sunday
echo date("Y-m-d", strtotime('monday this week', strtotime($date))), "\n";
echo date("Y-m-d", strtotime('sunday this week', strtotime($date))), "\n";
2015-03-16
2015-03-22
2015-03-23
2015-03-29
2015-03-16
2015-03-22
2015-03-23
2015-03-22
2015-03-23
2015-03-22
2015-03-23
2015-03-29
2015-03-23
2015-03-29
2015-03-30
2015-03-29
strtotime("next Monday") - 604800;
<?php
$weekstart = strtotime('monday this week');
$weekstop = strtotime('sunday this week 23:59:59');
//echo date('d.m.Y H:i:s', $weekstart) .' - '. date('d.m.Y H:i:s', $weekstop);
?>
$startOfWeek = (date('l') == 'Monday') ? date('d/m/Y 00:00') : date('d/m/Y', strtotime("last monday 00:00"));
$endOfWeek = (date('l') == 'Sunday') ? date('d/m/Y 23:59:59') : date('d/m/Y', strtotime("sunday 23:59:59"));
$date = '2015-10-12';
$day = date('N', strtotime($date));
$week_start = date('Y-m-d', strtotime('-'.($day-1).' days', strtotime($date)));
$week_end = date('Y-m-d', strtotime('+'.(7-$day).' days', strtotime($date)));
echo date("M-d-y", strtotime('last monday', strtotime('next week', time())));
class DateTimeUtilities {
public static function getPeriodFromMondayUntilFriday($offset = 'now') {
$now = new \DateTimeImmutable($offset, new \DateTimeZone('UTC'));
$today = $now->setTime(0, 0, 1);
$daysFromMonday = $today->format('N') - 1;
$monday = $today->sub(new \DateInterval(sprintf('P%dD', $daysFromMonday)));
$saturday = $monday->add(new \DateInterval('P5D'));
return new \DatePeriod($monday, new \DateInterval('P1D'), $saturday);
}
}
foreach (DateTimeUtilities::getPeriodFromMondayUntilFriday() as $day) {
print $day->format('c');
print PHP_EOL;
}
foreach (DateTimeUtilities::getPeriodFromMondayUntilFriday('2017-01-02T15:05:21+00:00') as $day) {
print $day->format('c');
print PHP_EOL;
}
//prints
//2017-01-02T00:00:01+00:00
//2017-01-03T00:00:01+00:00
//2017-01-04T00:00:01+00:00
//2017-01-05T00:00:01+00:00
//2017-01-06T00:00:01+00:00
$monday = DateTimeUtilities::getPeriodFromMondayUntilFriday('2017-01-02T15:05:21+00:00')->getStartDate()->format('c');
print $monday;
// prints
//2017-01-02T00:00:01+00:00
$firstDay = (new \DateTime())->modify(sprintf('-%d day', date('w') + 7))
->setTime(0, 0, 0);
$lastDay = (new \DateTime())->modify(sprintf('-%d day', date('w') + 1))
->setTime(23, 59, 59);
$string_date = '2019-07-31';
echo $day_of_week = date('N', strtotime($string_date));
echo $week_first_day = date('Y-m-d', strtotime($string_date . " - " . ($day_of_week - 1) . " days"));
echo $week_last_day = date('Y-m-d', strtotime($string_date . " + " . (7 - $day_of_week) . " days"));