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
https://forums.phpfreaks.com/topic/36548-time-question/#findComment-174016
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
https://forums.phpfreaks.com/topic/36548-time-question/#findComment-174028
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
https://forums.phpfreaks.com/topic/36548-time-question/#findComment-174034
Share on other sites

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.