加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

php日期的麻烦

发布时间:2020-12-13 16:17:57 所属栏目:PHP教程 来源:网络整理
导读:我试图根据今天的日期(2012年8月24日)计算以下三个值: 本月第一个星期六的日期. 本月第三个星期六的日期. 下个月第一个星期六的日期. 这就是我在PHP脚本中的做法: // Returns August 2012$this_month = date("F Y");// Returns September 2012$next_month
我试图根据今天的日期(2012年8月24日)计算以下三个值:

>本月第一个星期六的日期.
>本月第三个星期六的日期.
>下个月第一个星期六的日期.

这就是我在PHP脚本中的做法:

// Returns August 2012
$this_month = date("F Y");

// Returns September 2012
$next_month = date("F Y",strtotime("next month"));

// Returns August 04th,Saturday
$t_sat_1st=date('U',strtotime($this_month.' First Saturday'));

// Returns August 18th,Saturday
$t_sat_3rd=date('U',strtotime($this_month.' Third Saturday'));

// Returns September 08th,Saturday
$n_sat_1st=date('U',strtotime($next_month.' First Saturday') );

为什么最后一行代码返回了错误的日期?我希望它能在2012年9月1日返回.我的代码出了什么问题?

解决方法

我不知道为什么你的代码不完全正常工作,必须是解析它的方式..
但试试

$n_sat_1st=date('U',strtotime('first saturday of ' . $next_month) )

注意’有’是必要的.
此代码仅适用于5.3

It should be noted that apparently some of these strings only work in PHP 5.3 apparently,notably:

“first day of this month” and “last day of this month” for example.
According to information found on another website,the “xxx day of”
feature was added in PHP 5.3.

对此页面的评论 – http://www.php.net/manual/en/datetime.formats.relative.php

我已经在windows和ubuntu上测试了这个php 5.3和5.4并且它可以工作.

如果这不起作用,试试这个

$d = new DateTime($next_month);
 $d->modify('first saturday of this month');
 echo $d->format('U');

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读