3

I am trying to figure out what is the best way to

  1. Grab the current date/time (is date('Y-m-d H:i:s') best?)
  2. Grab the db date from SQL
  3. Check if the current date (#1) is between the date pulled from the DB and 5hours after it

I'm a newbie at php.

2 Answers 2

3

Pull the date you wanted from MySQL

  SELECT myDateCol FROM myTable WHERE ...

Convert this into a UNIX timestamp

$db_timestamp = strtotime($db_row['myDateCol']);

Check if the date from the DB is within the last 5 hours

if ($db_timestamp >= strtotime('-5 hours')) echo 'DB date is within last 5 hours';
Sign up to request clarification or add additional context in comments.

Comments

0

Use PHP's DateTime class:

$start = new DateTime("2009/09/17 18:52:31");
//Your SQL request should set this

$end = date_add($start,new DateInterval("PT5H"))

$now = new DateTime();

if($start<$now && $now<$end)
{
//etc
}

2 Comments

He said five hours, not five minutes.
Actually, the M stands for months. Well spotted though. I wasn't paying intention. Copy and paste finger strikes again...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.