mySQL Time, PHP Time, MODX Time

I found this to be quite handy.  Especially when you're trying to figure out why your MODX time is wonked.  Finding the mySQL time on some servers isn't always easy, but this will work on any server from within MODX.

Many thanks to Whistlemaker (Bill Cullingford) for writing this snippet and posting on the MODX Community Forum.

A simple snippet for testing that will display
  1. What time php thinks it is
  2. What time mySQL thinks it is
  3. What timezones are set for the mySQL server
    • and session (if different)

It assumes you have not moved the core.

//get the values we need to log into the mySQL server with
include 'core/config/';
//php date
$arr['php'] = date("F j, Y, g:i a");
$link = mysql_connect($database_server, $database_user, $database_password);
if (!$link) {
    die('Could not connect: ' . mysql_error());
if (!mysql_select_db($dbase)) {
    die('Could not select database: ' . mysql_error());
//get the mySQL time
$result = mysql_query("SELECT NOW();");
if (!$result) {
    die('Could not query:' . mysql_error());
$arr['time'] = mysql_fetch_array($result);
//returns the system timezone.
$result = mysql_query("SELECT @@system_time_zone;");
if (!$result) {
    die('Could not query:' . mysql_error());
$arr['systimezone'] = mysql_fetch_array($result);
//returns the session timezone if it differs from the system timezone.
$result = mysql_query("SELECT IF(@@session.time_zone = 'SYSTEM', @@system_time_zone, @@session.time_zone);");
if (!$result) {
    die('Could not query:' . mysql_error());
$arr['sestimezone'] = mysql_fetch_array($result);
$times = 'php time is: '.$arr['php'].'
$times .= 'mySQL time is: '.$arr['time'][0].'
$times .= 'mySQL system timezone is: '.$arr['systimezone'][0].'
$times .= 'mySQL session timezone is: '.$arr['sestimezone'][0].'
return $times;

To use this snippet, copy the PHP code above into a new snippet in MODX. Name it "dateTimeTest" and save.

Then create a new document/resource, with no template selected and tick "published" and "hide from menus".

In the content area insert the snippet call uncached , and save. View your resource in a browser window. Voila!

If you plan to use this more than once, make sure the snippet is called uncached. Otherwise, until you clear your cache, every view of the page will show the time the snippet initially fired.

Comments (1)

  1. Jamison mergens:
    Aug 31, 2012 at 10:06 AM

    For just server time:

Add a Comment

Allowed tags: <b><i><br>Add a new comment: