Skip to main content

Dates using PHP

Submitted by amitsedai on
Time and again date parsing issue comes up and I find myself looking all over the net for finding ways to do the same. This post is created to compile all such ways as I encounter: Parsing Dates If your date format is: yyyy-mm-dd, you can simply use strtotime and date function to get the desired date format $data[] = date( 'd-m-Y', strtotime('2012-03-21')); //get the format as 21-03-2012
Get Dates Using Strotime strtotime can recognize certain keywords. <?php echo strtotime('now'); echo strtotime('+4 days'); echo strtotime('+1 month'); echo strtotime('next monday'); echo strtotime('+2 weeks 3 days 4 hours 23 minutes'); ?> Compare Dates: <?php // your first date coming from a mysql database (date fields) $dateA = '2008-03-01 13:34'; // your second date coming from a mysql database (date fields) $dateB = '2007-04-14 15:23'; if(strtotime($dateA) > strtotime($dateB)){ // bla bla } ?> Get First and Last Date of a Month: <?php $first_date_curr = date('Y-m-01'); //First Date of The Current Month $last_date_curr = date('Y-m-t'); //Last Date of The Current Month $first_date_prev = date('Y-m-01', strtotime('-1 month')); //First Date of the Previous Month $last_date_prev = date('Y-m-t', strtotime('-1 month')); //Last Date of the Previous Month ?> Source: http://www.thetricky.net/php/Compare%20dates%20with%20PHP

Technologies