How to get the timestamp of the first day of the month in PHP

These code snippets show you how to get the timestamp of the first day of month in PHP using the PHP date function. 

The date function in PHP makes it really easy to obtain specific results with plain english. This is emphasised in this example where we can actually ask for 'first day of *month* *year*'. 

Here's an example of retrieving a timestamp of the first day of the month, where we fill the text gap with the current month and year represented by 'F Y' (which would return for example August 2014):

echo strtotime( 'first day of ' . date( 'F Y'));

This would produce a 11 digit timestamp like '1406851200'.

If you wanted to, you could now re-use the date function to re-format the timestamp:

echo date( 'd-m-Y', strtotime( 'first day of ' . date( 'F Y')));

This would produce a formatted date like '01-08-2014'.

You can read all the options available in the PHP documentation.

Sign Up

NEXT: Build an HTML5 responsive contact form

In this tutorial we'll be using the latest HTML5 form attributes with modern CSS responsive techniques to create a flexible, cross-platform contact form.

comments powered by Disqus
Sign Up

Popular Tags

350x250

Need a web developer?

If you'd like to work with code synthesis on your next project get in touch via the contact page.