如何使用PHP查找日期中某一天的日期?

时间:2022-10-21 12:58:48

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.

如果我有一个$ date YYYY-mm-dd并希望获得YYYY-mm-dd所在周的特定$ day(由0(星期日)到6(星期六)指定)。

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

例如,如果我把2012-10-11作为$ date和5作为$ day,我想得到2012-10-12,如果我有0作为$ day,2012-10-14

EDIT:
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,并希望得到同一周0-6指定的日期$ date。

So no, I don't want the day of $date...

所以不,我不想要约会日...

6 个解决方案

#1


68  

I think this is what you want.

我想这就是你想要的。

$dayofweek = date('w', strtotime($date));
$result    = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));

#2


46  

You can use the date() function:

您可以使用date()函数:

date('w'); // day of week

or

要么

date('l'); // dayname

Example function to get the day nr.:

得到一天的示例函数nr。:

function getWeekday($date) {
    return date('w', strtotime($date));
}

echo getWeekday('2012-10-11'); // returns 4

#3


8  

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)));

#4


2  

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'就是你想要的。

#5


0  

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);

#6


0  

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')

#1


68  

I think this is what you want.

我想这就是你想要的。

$dayofweek = date('w', strtotime($date));
$result    = date('Y-m-d', strtotime(($day - $dayofweek).' day', strtotime($date)));

#2


46  

You can use the date() function:

您可以使用date()函数:

date('w'); // day of week

or

要么

date('l'); // dayname

Example function to get the day nr.:

得到一天的示例函数nr。:

function getWeekday($date) {
    return date('w', strtotime($date));
}

echo getWeekday('2012-10-11'); // returns 4

#3


8  

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)));

#4


2  

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'就是你想要的。

#5


0  

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);

#6


0  

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')