Store utc date time on mysql

Example of the differences between timezones and the use of a function to convert "date()" and "DateTime()" from utc to another timezone or change "DateTime()" timezone with setTimeZone()

Set timezone:
<?php
$date=new DateTime("NOW"); 
echo $date->format('M j Y g:i:s a e');    //Mar 6 2013 11:14:58 pm Europe/Berlin

$date=new DateTime("NOW", new DateTimeZone('UTC')); 
echo $date->format('M j Y g:i:s a e');    //Mar 6 2013 10:14:58 pm UTC

echo date('M j Y g:i:s a e');            //Mar 6 2013 11:14:58 pm Europe/Berlin

date_default_timezone_set('UTC');
echo date('M j Y g:i:s a e');            //Mar 6 2013 10:14:58 pm UTC
?>

Database Table

Store UTC datetime:
<?php
date_default_timezone_set('UTC');
$date=new DateTime("NOW", new DateTimeZone('UTC')); 
//mysql with date()
$link= mysql_connect('SERVER','USER','PASS');
if(!$link){
   die('Not connected : ' . mysql_error());
}
$db=mysql_select_db('DATABASE', $link);
if(!$db){
   die('Can\'t use database : '.mysql_error());
}
$query=mysql_query("INSERT INTO TABLE (utc_date_time) VALUES ('".date('Y-m-d H:i:s')."')");
if (!$query){
   echo 'Mysql Error: '.mysql_error();
}
mysql_close($link);

//mysqli object oriented with DateTime()
$mysqli= new mysqli('SERVER','USER','PASS','DATABASE');
if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}
$mysqli->query("INSERT INTO TABLE (utc_date_time) VALUES ('".$date->format('Y-m-d H:i:s')."')");
$mysqli->close();

//mysqli procedural style with date()
$link= mysqli_connect('SERVER','USER','PASS','DATABASE');
if(mysqli_connect_errno()){
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}
mysqli_query($link,"INSERT INTO TABLE (utc_date_time) VALUES ('".date('Y-m-d H:i:s')."')");
mysqli_close($link);
?>


Change timezone:
<?php
//date or DateTime,DateTimeZone,format (default mysql datetime format)
function utc_to_local($datetime, $time_zone,$format_string="Y-m-d H:i:s") 
{ 
 if($datetime instanceof DateTime){  //if $datetime is instance of DateTime don't set DateTimeZone
  $date = $datetime;
 }else{  //default DateTimeZone set to UTC
  $date = new DateTime($datetime, new DateTimeZone('UTC')); 
 }
 $date->setTimeZone(new DateTimeZone($time_zone));  //change timezone 
 return $date->format($format_string); 
} 

$date=new DateTime("NOW"); 
echo $date->format('M j Y g:i:s a e');   //Mar 7 2013 12:07:02 am Europe/Berlin
echo utc_to_local($date,'America/Mexico_City','M j Y g:i:s a e'); //Mar 6 2013 5:07:02 pm America/Mexico_City

$date=new DateTime("NOW", new DateTimeZone('UTC'));   
echo $date->format('M j Y g:i:s a e');                   //Mar 6 2013 11:07:02 pm UTC
echo utc_to_local($date,'Asia/Tokyo','M j Y g:i:s a e'); //Mar 7 2013 8:07:02 am Asia/Tokyo

$date=date("Y-m-d H:i:s");
echo $date;                  //2013-03-07 00:07:02

date_default_timezone_set('UTC');
$date=date("Y-m-d H:i:s");   
echo $date;                 //2013-03-06 23:07:02
echo utc_to_local($date,'Europe/London','M j Y g:i:s a e'); //Mar 6 2013 11:07:02 pm Europe/London

$date=date('M j Y g:i:s a e');
echo $date;                 //Mar 6 2013 11:07:02 pm UTC
echo utc_to_local($date,'Antarctica/South_Pole'); //2013-03-07 12:07:02

$date="Mar 6 2013 7:43:12 pm UTC";
echo $date;                //Mar 6 2013 7:43:12 pm UTC
echo utc_to_local($date,'Africa/Lagos'); //2013-03-06 20:43:12
?>

0 Comments

Fb Comments




Publicar un comentario