imNEWBIE
imNEWBIE

Reputation: 9

comparing system date/time to inserted date/time to database

I have this way of saving date in database:

(DROPDOWN)Month (DROPDOWN)Day (DROPDOWN)Year

And I have this way for saving time in database:

(TEXTBOX)Time (DROPDOWN)AM/PM

then it will be saved in the base like this:

+------------------+-----------+
|      date        |   time    |
+------------------+-----------+
| January 1, 2013  |  4:00 PM  |
+------------------+-----------+
| January 6, 2013  |  9:00 AM  |
+------------------+-----------+

How would I compare the date in the database to computer time with if else statement?

if ("date/time in database" == "date/time in computer")
{
  echo "something stuff.";
} 

Thanks in Advance.

Upvotes: 0

Views: 478

Answers (4)

rajkumar
rajkumar

Reputation: 11

$date_from_db = "January 1, 2013 4:00 PM";  //just concatenate the date and time from db 

if ($date_from_db == date('F j, Y h:i A')){   
    echo "write your code";
} 

OR 

if (strtotime($date_from_db) == strtotime(date('F j, Y h:i A'))){
    echo "write your code";
}

Upvotes: 0

Brock Hensley
Brock Hensley

Reputation: 3655

Not exact answer, but it could help you... I use these functions to store datetime in database as UTC, and convert to server time when displaying. It accounts for daylight savings time.

function convert_server_datetime_to_utc() {
  return $gmdate = gmdate("Y-m-d H:i:s");
}

function convert_utc_to_server_datetime($utc=false) {
    if (empty($utc)){return false;}
    $date = new DateTime($utc);
    $hours_offset = date("Z")/60/60;
    if(substr($hours_offset, 0, 1) == "-") {
        $hours_offset = substr($hours_offset, 1);
        $invert=1;
    }
    $offset = new DateInterval('PT'.$hours_offset.'H');
    if (!empty($invert)) {$offset->invert = 1;}
    $date->add($offset);
    return $date->format("Y-m-d H:i:s");
}

echo "convert_datetime_to_utc(): " . $d = convert_server_datetime_to_utc();
echo "<hr>convert_utc_to_datetime(): " . convert_utc_to_server_datetime($d);

Upvotes: 0

Emanuele
Emanuele

Reputation: 21

Convert dates in unix timestamps and then compare:

  if (strtotime($your-db-stored-date) == strtotime($now)) {
    //doSomething;
  } 

anyways be sure to save dates using the appropriate type in your database...

Upvotes: 0

alwaysLearn
alwaysLearn

Reputation: 6950

Use strtotime() for that

if (strtotime("January 1, 2013") == strtotime(date('F j, Y')))
{
   echo "something stuff.";
} 

DOCUMENTATION LINK : http://www.php.net/manual/en/function.strtotime.php

GOOD LUCK!!

Upvotes: 0

Related Questions