我有一个活动,从四月的第二个星期三开始,到下一个星期日结束 - 每年。我需要在网站的标题中添加一些代码,以便在每个日历年之交自动更改日期。我读过一些几乎让我到达那里的东西,但没有找到任何完全有效的东西。
有人对此有任何解决方案吗?
日期格式如下所示:2014 年 4 月 9 日至 13 日
谢谢。
<?php
$start = new DateTime('second wednesday of april');
$end = clone $start;
$end->modify('+4 days');
echo $start->format('Y-m-d') . ' ' . $end->format('Y-m-d');
?>
您可以将相对格式与 PHP 的strtotime
一起使用。在您的示例中:
$next_year = strtotime( '%G', strtotime( '+1 Year' ));
$start = strtotime( 'Second Wednesday of April ' . $next_year ));
$end = strtotime( '+4 Days', $start );
使用该开始日期和结束日期,您可以使用strftime
根据需要设置最终结果的格式:
echo strftime('%F', $start) . ' - ' . strftime('%F', $end);