Reputation: 15
I have script
that run a php
every minute. I want to make a variable that store the current hour everytime its runs so i can make an if()
statement so when its 7pm execute the code inside.
$hour = date('H');
If($hour >= 7:00pm <= 5:00am)
{Do something that i know how to do it xD}
Please no Cron Job or similar.
Upvotes: 0
Views: 134
Reputation: 5135
@Miguel By default, PHP took the time from server where its located. You can set the default time in your common PHP file(which includes in all pages or at-least in that page, in which you wrote the code to execute). using the below function, you can set the default time zone for your web application.
date_default_timezone_set('America/Los_Angeles');
here is the list of Timezones (http://php.net/manual/en/timezones.php), select according to your desire country/city and pass that value in the above function.
Upvotes: 0
Reputation: 11557
You have several options, something rather alternative that might be a better solution depending on what you are trying to do, is to just setup the script to run everyday at 7PM. This can be done via cron jobs in linux with the following command:
0 18 * * * php -q /path/to/php/script.php
This will be based on your server time and timezone. Based on your comment on Emz's post, You should know that when you track the time with php you need to realize that php.ini can change the time in php scripts as it has it's own timezone!
Upvotes: 0
Reputation: 61
Sounds like this might be handled better via a cron. Just a thought.
Upvotes: 0
Reputation: 1280
date('H');
will return the current hour.
You can then use it in an if
-statement.
It is hard to provide more than this as you showed very little of how and why you want to perform this check.
Upvotes: 1