简体   繁体   中英

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.

I tried echo date('Ym-d', strtotime('first day of last month')); from this question but it returned 1970-01-01 instead.

Can someone explain to me why the code return date like that?

this must work even on older versions of 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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