Get the first day of the previous month at 00:00:00 with DateTime?

How can I get the first day of the previous month to start? I am currently using this:

$date = new DateTime('first day of last month'); 

This gives me the first day of last month, but in relation to my current time. What I'm actually trying to do is start from the time zone. For example:

 $date = new DateTime('first day of previous month', new DateTimeZone('UTC')); 

The result will be July 1, 2013 00:00:00. Or if I use:

 $date = new DateTime('first day of previous month', new DateTimeZone('Europe/Amsterdam')); 

Expected Result: June 30, 2013, 9:00 p.m. (due to its displacement).

How to do it using PHP?

+7
php datetime
source share
1 answer

Just add time ( time formats )

 $date = new DateTime('first day of previous month 00:00:00', new DateTimeZone('UTC')); var_dump($date->format('Ymd H:i:s')); // string(19) "2013-11-01 00:00:00" $date = new DateTime('first day of previous month 00:00:00', new DateTimeZone('Europe/Amsterdam')); var_dump($date->format('Ymd H:i:s')); // string(19) "2013-11-01 00:00:00" 

Or add midnight ( Relative formats )

 $date = new DateTime('midnight first day of previous month', new DateTimeZone('Europe/Amsterdam')); var_dump($date->format('Ymd H:i:s')); $date = new DateTime('midnight first day of previous month', new DateTimeZone('UTC')); var_dump($date->format('Ymd H:i:s')); 

Demo

+5
source share

All Articles