In a script that contains
date('Y-m-d', strtotime('first day of last month'))
in version 5.3.10 (localhost) I get, for example, '2012-03-01'.
in version 5.2.17 (remote host) I get '1969-12-31'.
Is there an expression that will return expected (e.g., '2012-03-01') results for both versions?
php $query_date = '2010-02-04'; // First day of the month. echo date('Y-m-01', strtotime($query_date)); // Last day of the month. echo date('Y-m-t', strtotime($query_date));
To get the first and last day of the current month, use the getFullYear() and getMonth() methods to get the current year and month and pass them to the Date() constructor to get an object representing the two dates.
The strtotime() function parses an English textual datetime into a Unix timestamp (the number of seconds since January 1 1970 00:00:00 GMT). Note: If the year is specified in a two-digit format, values between 0-69 are mapped to 2000-2069 and values between 70-100 are mapped to 1970-2000.
You should use the mktime() function:
<?php
echo date('Y-m-d', mktime(0,0,0,date('n')-1,1,date('Y'))); //2012-03-01
?>
See In Action
date('Y-m-d', strtotime('first day of -1 month'))
Works fine on PHP 7.0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With