如何从使用PHP的日期中找到星期几? [英] How to find the day of week from a date using PHP?
问题描述
如果我有一个 $ date
YYYY-mm-dd
,并希望获得一个特定的<$ YYYY-mm-dd
的星期的c $ c> $ day (由0(星期日)到6(星期六)指定)
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
as $ 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
,并希望得到同一周0-6的指定日期 $ date
在...中。
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
...
推荐答案
我想这就是你想要的。
$dayofweek = date('w', strtotime($date));
$result = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));
这篇关于如何从使用PHP的日期中找到星期几?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!