user1675769
user1675769

Reputation: 9

How do I test if mysql timestamp is during the day (between 8:00 and 20:00) with php?

I'm making a page that displays some info in a table and I want that table to have a darker background on the entries from the night time.

I'm thinking of something like:

    if ( ($timestamp < strtotime('2012-02-16 20:00:00') && ($timestamp > strtotime('2012-02-16 08:00:00') ){}

But I want this to function 24/7 all year. How do I make the date any date (* - * - *) ?

Upvotes: 0

Views: 492

Answers (4)

Travesty3
Travesty3

Reputation: 14489

Not sure what format $timestamp is in, but if it's in Unix timestamp format (epoch seconds), you could do this:

$hour = date('H', $timestamp);
if ($hour >= 8 && $hour < 20)

If it's not in Unix timestamp format, perhaps you could make it so in your query, using the UNIX_TIMESTAMP() function.

Upvotes: 4

Cram Pisam
Cram Pisam

Reputation: 120

Maybe you can format date with only hours component like

int(date('H', $timestamp ))

and test this value

Upvotes: 0

Dmitry Kurilo
Dmitry Kurilo

Reputation: 404

Use also can use mktime() function to make the timestamp.

For example, today, 8PM:

$mktime = mktime(20, 0, 0, date(n), date(j), date(Y)); 

Upvotes: 0

SubjectCurio
SubjectCurio

Reputation: 4882

$curtime = date('G', $timestamp);
if ($curtime >= 8 && $curtime <= 20)
{
  //do whatever
}

I'd recommend reading up on date()

Upvotes: 1

Related Questions