如何使用PHP从日期中查找星期几的日期? [英] How to find the date of a day of the week from a date using PHP?
问题描述
如果我有一个 $date
YYYY-mm-dd
并且想要获得一个特定的 $day
(由 0 (YYYY-mm-dd
所在周的星期日) 到 6 (saturday)).
If I've got a $date
YYYY-mm-dd
and want to get a specific $day
(specified by 0 (sunday) to 6 (saturday)) of the week that YYYY-mm-dd
is in.
例如,如果我将 2012-10-11
作为 $date
而将 5
作为 $day
, 我想得到 2012-10-12
, 如果我有 0
作为 $day
, 2012-10-14
For example, if I got 2012-10-11
as $date
and 5
as $day
, I want to get 2012-10-12
, if I've got 0
as $day
, 2012-10-14
你们大多数人都误解了它.我得到了一些日期,$date
并且想要获得 $date
所在的同一周的 0-6 指定的一天.
Most of you misunderstood it. I got some date, $date
and want to get a day specified by 0-6 of the same week $date
is in.
所以不,我不想要 $date
...
So no, I don't want the day of $date
...
推荐答案
我认为这就是您想要的.
I think this is what you want.
$dayofweek = date('w', strtotime($date));
$result = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));
这篇关于如何使用PHP从日期中查找星期几的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!