Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Version difference for strtotime('first day of last month')?

Tags:

date

php

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?

like image 895
geoB Avatar asked Apr 20 '12 16:04

geoB


People also ask

How can get first and last day of month in php?

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));

How do I find the first and last date of the month?

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.

Is Strtotime a second?

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.


2 Answers

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

like image 105
Lawrence Cherone Avatar answered Sep 17 '22 08:09

Lawrence Cherone


date('Y-m-d', strtotime('first day of -1 month'))

Works fine on PHP 7.0

like image 27
Oleg Avatar answered Sep 19 '22 08:09

Oleg