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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.