user702300
user702300

Reputation: 1241

PHP check if after midnight

How do I check after midnight? For example, I have this code

// Run cron job if passed 24 hours (86400 seconds), 
// but only if it's after midnight

if ( (time() - $LastUpdate) >= 86400 && PAST_MIDNIGHT ) {
      do something ...
}

How do I fill in PAST_MIDNIGHT? My intention is to run a cron job once per day, but only after midnight. Thank you.

Upvotes: 1

Views: 2106

Answers (3)

Leonardo
Leonardo

Reputation: 736

If you are using a unix system, you can use the cron service:

Run this in your console:

crontab -e

And then add the following at the last line:

@daily /full/path/to/php /full/path/to/your/script.php

This will execute every day at midnight

Upvotes: 0

TunaMaxx
TunaMaxx

Reputation: 1769

Kind of hackish, but this with check if the current server time hour (in 24 hour format) is less than 7. So, anywhere from midnight (0) to 7:00am (7) will pass. Other times won't.

if ( (time() - $LastUpdate) >= 86400 && (date('G') < 7)) {
  do something ...
}

See http://www.php.net/manual/en/function.date.php for a list of parameters you can throw at date().

Upvotes: 2

phil-lavin
phil-lavin

Reputation: 1227

Given that midnight is the first second of the day, it's never not 'past midnight'.

Since you're using cron, have it scheduled to run at the time you need it to run.

Upvotes: 2

Related Questions