Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get timestamp of current month, first day of month, zero hour

I am trying to get the timestamp of the first day of the month at 00:00:00.

For example, the timestamp of Jan 01 2012 00:00:00

I'm doing this:

$test_month = time(); // Seconds $test_month = date('M', $test_month); // This month $test_month = strtotime($test_month); // Timestamp of this month echo $test_month; 

This is returning zero hour of the current day of the month, not the start of the month.

Any suggestions?

like image 795
MultiDev Avatar asked Jun 28 '12 00:06

MultiDev


People also ask

How can I get first day of current month in php?

php $dt = "2008-02-23"; echo 'First day : '. date("Y-m-01", strtotime($dt)).

How do you add a month to a timestamp?

I would use the strtotime() function, to add +1 month to your timestamp : $future = strtotime('+1 month', $current_time);

How can I get first date and date of last month in php?

php echo 'First Date = ' . date('Y-m-01') . '<br />'; echo 'Last Date = ' . date('Y-m-t') .


2 Answers

Try this:

echo date( 'F jS Y h:i:s A', strtotime( 'first day of ' . date( 'F Y'))); 

This will output:

June 1st 2012 12:00:00 AM 
like image 50
nickb Avatar answered Sep 22 '22 13:09

nickb


Try this

$time = strtotime(date('Y-m-01 00:00:00')); // == 1338534000 

Which translates to:

$date = date('Y-m-d H:i:s', $time); // == 2012-06-01 00:00:00 

Read the PHP manual on the date() and time() functions.

like image 29
sesser Avatar answered Sep 22 '22 13:09

sesser