[英]How to get first day of last month?
I need to get the first day of last name everyday. 我需要每天获取姓氏的第一天。 I used to code it like this:
我曾经这样编码:
echo date('Ym01',strtotime('last month'));
But today (March 30,2017), it returned the first day of this month, not last month. 但是今天(2017年3月30日),它返回了本月的第一天,而不是上个月。
I tried echo date('Ym-d', strtotime('first day of last month'));
我试过了
echo date('Ym-d', strtotime('first day of last month'));
from this question but it returned 1970-01-01
instead. 从这个问题,但它返回
1970-01-01
。
Can someone explain to me why the code return date like that? 有人可以向我解释为什么这样的代码返回日期吗?
this must work even on older versions of php 即使在旧版本的php上也必须工作
<?php
$lastmonth= new datetime();
//formating the date
$lastmonth=$lastmonth->format("Ym01");
//subtracting 1 month to make it last month
$lastmonth=date('Ym01',strtotime($lastmonth . '-1 month'));
echo $lastmonth;
?>
If it doesn't work then the problem is with your server 如果不起作用,则问题出在服务器上
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.