I searched around but couldn’t find exactly what I’m looking for.
I have a PHP Settings page where Admins from my site are able to select an hour of the day for a daily Cron Job script to execute. In my crontab I have the script running every hour. When the script runs, it checks to see if the current hour is equal to the hour that the settings page requested.
The reason I did it like this is because I absolutely could not figure out a way to edit the crontab file via PHP, despite doing a lot of testing and research on the subject matter. Bonus points if you can help me figure out a way to do this.
The real reason I am creating this question is because I am having some issues with my Unix server’s ` time. When on my localhost machine (IIS), the Cron script is executing each hour like it’s supposed to, but when I moved everything over to my Unix Amazon machine, the time is off by exactly five hours.
The Cron job is still running hourly like it’s supposed to, but when the PHP script checks to see if the current time (using date("G")) matches the settings time, it will not work like intended. Sure, it will eventually get processed exactly five hours too late.
I don’t think it’s a coincidence that I live in the Central timezone, which also happens to be five hours off, so I’m nearly positive it is related to that.
I could write a work-around that will always offset the time by five hours, but this will cause my date fields to be inaccurate (I think) and it’s kind of a stretch. I’d rather just ask on SO because I’m sure that a lot of people are reading this and thinking that I’m stupid for not knowing the answer.
One more thing:
I have this line of code in my scripts. Anything else I need to share?
date_default_timezone_set('America/Chicago');
EDIT: An explanation of what your “fix” is would be greatly appreciated.
There are a few questions being asked here I’d say, to answer them all would take some time, but I’ll try to skim through as much as possible.
$sLinuxDate = shell_exec('date');. Probly you want to get the timezone between PHP & the OS set correctly via php.ini as I mentioned though.