Jump to content

[SOLVED] I need some guidance on comparing dates


jim.davidson

Recommended Posts

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.

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';
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.