简体   繁体   English

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

[英]How to find the date of a day of the week from a date using PHP?

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 ,并希望得到一个特定的$day (0(星期日指定)至6日(星期六))在本周的YYYY-mm-dd在不在。

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作为$date5作为$day ,我想得到2012-10-12 ,如果我有0作为$day2012-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 ... 所以不,我不想要$date ...

I think this is what you want. 我想这就是你想要的。

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

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

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

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

If your date is already a DateTime or DateTimeImmutable you can use the format method. 如果您的日期已经是DateTimeDateTimeImmutable ,则可以使用format方法。

$day_of_week = intval($date_time->format('w'));

The format string is identical to the one used by the date function. 格式字符串与date函数使用的格式字符串相同。


To answer the intended question: 要回答预期的问题:

$date_time->modify($target_day_of_week - $day_of_week . ' days');

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

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

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)
<?php echo date("H:i", time()); ?>
<?php echo $days[date("l", time())] . date(", d.m.Y", time()); ?>

Simple, this should do the trick 很简单,这应该可以解决问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM