How can I find the first and last date in a month using PHP? For example, today is April 21, 2010; I want to find April 1, 2010 and April 30, 2010.
php function days($date1, $date2) { $date1 = strtotime($date1); $date2 = strtotime($date2); return ($date2 - $date1) / (24 * 60 * 60); } $date1 = '20100820'; $date2 = '20100930'; echo days($date1, $date2); ?>
$date = strtotime ( $datestring ); // Last date of current month. $day = date ( "l" , $lastdate );
$monday = date('d-m-Y',strtotime('last monday',strtotime('next monday',strtotime($date)))); You have to get next monday first then get the 'last monday' of next monday. So if the given date is monday it will return the same date not last week monday.
php $next_due_date = date('y-m-d',strtotime('+30 days',strtotime('echo $userRow3["due_date"]'))) .
The easiest way is to use date
, which lets you mix hard-coded values with ones extracted from a timestamp. If you don't give a timestamp, it assumes the current date and time.
// Current timestamp is assumed, so these find first and last day of THIS month $first_day_this_month = date('m-01-Y'); // hard-coded '01' for first day $last_day_this_month = date('m-t-Y'); // With timestamp, this gets last day of April 2010 $last_day_april_2010 = date('m-t-Y', strtotime('April 21, 2010'));
date()
searches the string it's given, like 'm-t-Y'
, for specific symbols, and it replaces them with values from its timestamp. So we can use those symbols to extract the values and formatting that we want from the timestamp. In the examples above:
Y
gives you the 4-digit year from the timestamp ('2010')m
gives you the numeric month from the timestamp, with a leading zero ('04')t
gives you the number of days in the timestamp's month ('30')You can be creative with this. For example, to get the first and last second of a month:
$timestamp = strtotime('February 2012'); $first_second = date('m-01-Y 00:00:00', $timestamp); $last_second = date('m-t-Y 12:59:59', $timestamp); // A leap year!
See http://php.net/manual/en/function.date.php for other symbols and more details.
Simple one
Reference - http://www.php.net/manual/en/function.date.php
<?php echo 'First Date = ' . date('Y-m-01') . '<br />'; echo 'Last Date = ' . date('Y-m-t') . '<br />'; ?>
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