jim.davidson Posted April 4, 2008 Share Posted April 4, 2008 I have an MySQL table(articles) that has a field named (date_written) that is a date field formatted yyyy-mm-dd. I get a recordset for a record in articles: getArticle and I store the date to variable $date_written = $row_GetArticle['date_written'] I need to know using php how to compare that date to today's date to see if it's at least a year old. Any help will be appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/99589-solved-i-need-some-guidance-on-comparing-dates/ Share on other sites More sharing options...
GingerRobot Posted April 4, 2008 Share Posted April 4, 2008 When comparing dates, convert them to unix timestamps(the number of seconds since 1st Jan 1970) first: <?php $date_written = $row_GetArticle['date_written']; $timestamp = strtotime($date_written); $now = time(); if($now-$timestamp > 60*60*24*365){ echo 'Article was written more than a year ago'; }else{ echo 'Article is less than a year old'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/99589-solved-i-need-some-guidance-on-comparing-dates/#findComment-509482 Share on other sites More sharing options...
jim.davidson Posted April 4, 2008 Author Share Posted April 4, 2008 Thanks! I'll give it a go. Quote Link to comment https://forums.phpfreaks.com/topic/99589-solved-i-need-some-guidance-on-comparing-dates/#findComment-509488 Share on other sites More sharing options...
craygo Posted April 4, 2008 Share Posted April 4, 2008 <?php $date_written = strtotime($row_GetArticle['date_written']); $today = strtotime("Now"); if(($today - $date_written) > 31556926){ echo "over a year old"; } else { echo "Under a year old"; } ?> Ray Quote Link to comment https://forums.phpfreaks.com/topic/99589-solved-i-need-some-guidance-on-comparing-dates/#findComment-509491 Share on other sites More sharing options...
laffin Posted April 4, 2008 Share Posted April 4, 2008 <?php header('Content-type: text/plain'); $date_written = strtotime($row_GetArticle['date_written']); if($date_written < strtotime('-1 Year')) echo "Over"; else echo "Under"; echo " a year old"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/99589-solved-i-need-some-guidance-on-comparing-dates/#findComment-509534 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.