Jump to content

Time Question


MadDawgX

Recommended Posts

ok, the easiest way to compare dates is to use unix timestamp format.

 

So you have two ways to do this...

 

1) Get the date from your database, use php to convert it to a Unix timestamp and then compare it to the current timestamp.

2) Use MySQL's timestamp function and just compare it in php.

 

The first is longer, but the column type in MySQL doesn't matter too much

The second is easier and involves changing your sql query very slightly, but is dependent on the column type in MySQL being either, date, or datetime.

 

Which way do you think you'd like to go?

 

Regards

Huggie

Link to comment
Share on other sites

There may be an easier way, but...

 

$databaseDate = strtotime(mysql_result($rTask,0,'due'));
$taskDue = $databaseDate;
if(date("Y-m-d") == date("Y-m-d", $taskDue)){
// it's today
}else if(time() > $taskDue){
// it's after today
}else{
  // it's before today
}

(not tested)

Link to comment
Share on other sites

In that case it's really simple...

 

I can see from your existing code you're column is called due, so my sql statement is only doing to select from that column, but you can put all of your columns in...

 

<?php
// Query database and have the due column returned as a unix_timestamp
$sql = "SELECT unix_timestamp(due) AS due FROM table_name WHERE column_name = 'condition'";
$result = mysql_query($sql) or die ("Unable to execute $sql: " . mysql_error());

// Get our timestamp into a php variable for comparison
$due_date = mysql_result($result,0,'due');

if (date("Y-m-d") == (date("Y-m-d", $due_date)){
   echo "On";
}
elseif (time() > $due_date){
   echo "After";
}
else {
   echo "Before";
}
?>

 

Edit: You're to quick for me Jesi

 

Regards

Huggie

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.