简体   繁体   English

php 如何将日期字符串转换为最近一周的日期

[英]php how to convert day string to the nearest date of current week

I want to show the date of the current week if the variable is the day string so if the variable is Friday then the date must be 2020-07-03 and if today is Thursday and the variable is Thursday then the date must be 2020-07-09如果变量是日期字符串,我想显示当前星期的日期,所以如果变量是星期五,那么日期必须是 2020-07-03,如果今天是星期四并且变量是星期四,那么日期必须是 2020- 07-09

$today= \Carbon\Carbon::today()->format('Y-m-d');

In core PHP, there is an awesome function called strtotime() .在核心 PHP 中,有一个很棒的 function 称为strtotime() It can be used to serve the purpose,它可以用来服务于目的,

$dayOfTheWeek = 'Wednesday';
$ts = strtotime('next ' . $dayOfTheWeek);
echo date('Y-m-d', $ts);

I hope this helps.我希望这有帮助。

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

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