user370306
user370306

Reputation:

Make an hour into timestamp

How can I make a time, for example 09, to a UNIX timestamp? I tried strtotime() but unsuccessfully. strtotime("9") returns nothing.

Edit:
It's my fault that I didn't mention before but I'm trying to check if current time + 30 minutes is bigger or equal to time, saved in a mysql DB. I save time in DB as 09, for example (probably there's my mistake).

Upvotes: 1

Views: 440

Answers (3)

Viktor
Viktor

Reputation: 3536

The additional information you provided in the edit clarifies one or two things. Generating a timestamp for comparison probably won't do much good if the database value is a string representing the hour. In this case, you can just as well compare the hour values as numbers:

// Value from database
$db_hour = '09';

// Hour 30 minutes from now
$compare_hour = date('H', strtotime('+30 minutes'));

// Check if current time + 30 minutes is bigger
if (intval($compare_hour) >= intval($db_hour))

If the database hour values are between 0 and 23, the comparison will of course always return true between 22:30 and 23:29.

Note: Had the MySQL table field been a timestamp field, you could get just the hour part with "SELECT EXTRACT(HOUR FROM your_field) AS db_hour FROM your_table;"

http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_extract

Upvotes: 0

No Results Found
No Results Found

Reputation: 102745

While both answers so far are probably better, I thought I'd offer a "cute" solution that still uses strtotime():

$hour = "09";
echo strtotime("Today $hour:00");

Demo: http://codepad.org/k9U4BiKN

Upvotes: 1

Luka
Luka

Reputation: 321

Try mktime

int mktime ([ int $hour = date("H") [, int $minute = date("i") [, int $second = date("s") [, int $month = date("n") [, int $day = date("j") [, int $year = date("Y") [, int $is_dst = -1 ]]]]]]] )

http://php.net/manual/en/function.mktime.php

Upvotes: 1

Related Questions