PHPRO.ORG

First Day Of Month

First Day Of Month

Here is an often requested function to fetch the first day of the month. The function takes a single, optional parameter which is a unix timestamp of any date. The function will then return the unix timestamp of the first day of the month from the UNIX TIMESTAMP. If no timestamp is given, the function defaults to the current month.

The resulting timestamp can then be used with the PHP date() function to manipulated in any way possible. Ideal for calander classes or anywhere the first day of a month is required.


<?php

/*
 *
 * @ return timestamp of the first day of the month
 *
 * @param INT Unix timestamp
 *
 * @return INT
 *
 */
function firstDayOfMonth($uts=null)
{
    
$today is_null($uts) ? getDate() : getDate($uts);
    
$first_day getdate(mktime(0,0,0,$today['mon'],1,$today['year']));
    return 
$first_day[0];
}

 
/*** example usage ***/

 /*** using the default ***/
 
echo firstDayOfMonth();

 echo 
'<hr />';

 
/*** using a timestamp ***/
 
$long_ago strtotime('April 16 2002');
 echo 
firstDayOfMonth($long_ago);

?>

Demonstration

1543651200
1017648000