note 77649 modified in function.time by danbrown

From: Date: Sun, 20 Jul 2008 04:12:22 +0000
Subject: note 77649 modified in function.time by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-142117@lists.php.net to get a copy of this message
A function for converting to Unix time without using the MySQL UNIX_TIMESTAMP function in a query (MySQL allows eight different formats for timestamps): <?php function UnixTime($mysql_timestamp){ if (preg_match('/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces) || preg_match('/(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)) { $unix_time = mktime($pieces[4], $pieces[5], $pieces[6], $pieces[2], $pieces[3], $pieces[1]); } elseif (preg_match('/\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/', $mysql_timestamp) || preg_match('/\d{2}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/', $mysql_timestamp) || preg_match('/\d{4}\-\d{2}\-\d{2}/', $mysql_timestamp) || preg_match('/\d{2}\-\d{2}\-\d{2}/', $mysql_timestamp)) { $unix_time = strtotime($mysql_timestamp); } elseif (preg_match('/(\d{4})(\d{2})(\d{2})/', $mysql_timestamp, $pieces) || preg_match('/(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)) { $unix_time = mktime(0, 0, 0, $pieces[2], $pieces[3], $pieces[1]); } return $unix_time; } ?> --was-- A function for converting to Unix time without using the MySQL UNIX_TIMESTAMP function in a query (MySQL allows eight different formats for timestamps): function UnixTime($mysql_timestamp){ if (preg_match('/(\d{4})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces) || preg_match('/(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)) { $unix_time = mktime($pieces[4], $pieces[5], $pieces[6], $pieces[2], $pieces[3], $pieces[1]); } elseif (preg_match('/\d{4}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/', $mysql_timestamp) || preg_match('/\d{2}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}/', $mysql_timestamp) || preg_match('/\d{4}\-\d{2}\-\d{2}/', $mysql_timestamp) || preg_match('/\d{2}\-\d{2}\-\d{2}/', $mysql_timestamp)) { $unix_time = strtotime($mysql_timestamp); } elseif (preg_match('/(\d{4})(\d{2})(\d{2})/', $mysql_timestamp, $pieces) || preg_match('/(\d{2})(\d{2})(\d{2})/', $mysql_timestamp, $pieces)) { $unix_time = mktime(0, 0, 0, $pieces[2], $pieces[3], $pieces[1]); } return $unix_time; } http://php.net/manual/en/function.time.php

« previous php.notes (#142117) next »