I'm guessing there's a difference between my PHP time and the server time.
When I check the current time in PHP, it's showing that MST is being used. However, cron jobs aren't running at the correct time.
How can I check to see what timezone the server itself is using, not what PHP is set to use?
Cron job uses the server's define timezone (UTC by default) which you can check by typing the
date
command in terminal.All countries timezones are defined in
/usr/share/zoneinfo
directory:When you cd into this directory you will see the name of different countries and their timezone.
Command to change server timezone.
If you live in America > LA you can change your time-zone using above command. Change the country and state according to your requirement.
Command to check the date and time:
Set time and date from the command line:
If you have may users, with many crontabs and they have different time zone requirements, you cannot just change system timezone.
But you still can set a specific time zone to be used just for the cron jobs in a specific crontab setting the variable CRON_TZ at the beginning of the crontab.
E.g.
While the accepted answer is correct, I would like to add a correction to the verification.
The accepted answer says you can verify the system time by typing date in the terminal.
When I type date, it tells me the time for the timezone I am in, which is central time. I tried the same command as root and it always returns my time zone. When I check the syslog it also shows the time I expected the job to execute.
I then opened the crontab which registers an entry in the syslog for a time in the future. This is how I verified the server was using UTC time. But to point out, at least in my case, using the command date does not verify the timezone executed by the cron.
Unless you know a priori what time zone cron is running in, the only reliable solution is to check empirically. Otherwise, you might just be checking the time zone of your current shell environment, or checking the system-wide time zone which might be different from cron's.
Here's a bash one-liner that tells you the time zone cron sees:
env -i $(cat /proc/$(</var/run/crond.pid)/environ|xargs -0 echo) date +%Z
This command creates an environment identical to that of the running cron daemon, and then runs
date
in that environment.