php 如何使用PHP从日期中查找星期几的日期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12835133/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find the date of a day of the week from a date using PHP?
提问by Zulakis
If I've got a $dateYYYY-mm-ddand want to get a specific $day(specified by 0 (sunday) to 6 (saturday)) of the week that YYYY-mm-ddis in.
如果我有一个$dateYYYY-mm-dd并且想要获得$day一周的特定(由 0(星期日)到 6(星期六)指定)YYYY-mm-dd。
For example, if I got 2012-10-11as $dateand 5as $day, I want to get 2012-10-12, if I've got 0as $day, 2012-10-14
例如,如果我得到2012-10-11as$date和5as $day,我想得到2012-10-12,如果我得到0as $day,2012-10-14
EDIT:
Most of you misunderstood it. I got some date, $dateand want to get a day specified by 0-6 of the same week $dateis in.
编辑:
你们大多数人都误解了它。我得到了一些日期,$date并希望获得同一周的 0-6 指定的一天$date。
So no, I don't want the day of $date...
所以不,我不想要$date...
回答by Rezigned
I think this is what you want.
我想这就是你想要的。
$dayofweek = date('w', strtotime($date));
$result = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));
回答by powtac
You can use the date() function:
您可以使用 date() 函数:
date('w'); // day of week
or
或者
date('l'); // dayname
Example function to get the day nr.:
获取日期的示例函数:
function getWeekday($date) {
return date('w', strtotime($date));
}
echo getWeekday('2012-10-11'); // returns 4
回答by air4x
Try
尝试
$date = '2012-10-11';
$day = 1;
$days = array('Sunday', 'Monday', 'Tuesday', 'Wednesday','Thursday','Friday', 'Saturday');
echo date('Y-m-d', strtotime($days[$day], strtotime($date)));
回答by Johannes Buchholz
If your date is already a DateTimeor DateTimeImmutableyou can use the formatmethod.
如果您的日期已经是DateTime或者DateTimeImmutable您可以使用该format方法。
$day_of_week = intval($date_time->format('w'));
The format string is identical to the one used by the datefunction.
格式字符串与日期函数使用的格式字符串相同。
To answer the intended question:
要回答预期的问题:
$date_time->modify($target_day_of_week - $day_of_week . ' days');
回答by lijinma
Just:
只是:
2012-10-11 as $date and 5 as $day
2012-10-11 作为 $date 和 5 作为 $day
<?php
$day=5;
$w = date("w", strtotime("2011-01-11")) + 1; // you must add 1 to for Sunday
echo $w;
$sunday = date("Y-m-d", strtotime("2011-01-11")-strtotime("+$w day"));
$result = date("Y-m-d", strtotime($sunday)+strtotime("+$day day"));
echo $result;
?>
The $result = '2012-10-12' is what you want.
$result = '2012-10-12' 就是你想要的。
回答by blue112
PHP Manual said :
PHP手册说:
w Numeric representation of the day of the week
w 星期几的数字表示
You can therefore construct a date with mktime, and use in it date("w", $yourTime);
因此,您可以使用 mktime 构造一个日期,并在其中使用 date("w", $yourTime);
回答by John Dvorak
I'm afraid you have to do it manually. Get the date's current day of week, calculate the offset and add the offset to the date.
恐怕你必须手动完成。获取日期的当前星期几,计算偏移量并将偏移量添加到日期。
$current = date("w", $date)
$offset = $day - $current
$new_date = new DateTime($date)
->add(
new DateInterval($offset."D")
)->format('Y-m-d')
回答by victorf
I had to use a similar solution for Portuguese (Brazil):
我不得不对葡萄牙语(巴西)使用类似的解决方案:
<?php
$scheduled_day = '2018-07-28';
$days = ['Dom','Seg','Ter','Qua','Qui','Sex','Sáb'];
$day = date('w',strtotime($scheduled_day));
$scheduled_day = date('d-m-Y', strtotime($scheduled_day))." ($days[$day])";
// provides 28-07-2018 (Sáb)
回答by Rune B?ge
<?php echo date("H:i", time()); ?>
<?php echo $days[date("l", time())] . date(", d.m.Y", time()); ?>
Simple, this should do the trick
简单,这应该可以解决问题

