Date and Time

Коментарии

Should you want to convert between HH:MM:SS and plain seconds like in MySQL, these functions should do the trick:

<?php
function time_to_sec($time) {
   
$hours substr($time0, -6);
   
$minutes substr($time, -52);
   
$seconds substr($time, -2);

    return 
$hours 3600 $minutes 60 $seconds;
}

function 
sec_to_time($seconds) {
   
$hours floor($seconds 3600);
   
$minutes floor($seconds 3600 60);
   
$seconds $seconds 60;

    return 
sprintf("%d:%02d:%02d"$hours$minutes$seconds);
}
?>
2008-10-24 18:52:43
http://php5.kiev.ua/manual/ru/book.datetime.html
We can also get the submitted datetime (e.g. 2009-11-06 07:03:41) using the following:

<?php
$DateOfRequest 
date("Y-m-d H:i:s"strtotime($_REQUEST["DateOfRequest"]));
?>

Or another good example of getting DateTime:

<?php
$DateOfRequest 
date("Y-m-d H:i:s"mktime($_REQUEST["Hour"],$_REQUEST["Min"],$_REQUEST
["Sec"],$_REQUEST["Month"],$_REQUEST["Day"],$_REQUEST["Year"]));           
?>
2009-10-12 07:59:38
http://php5.kiev.ua/manual/ru/book.datetime.html
Автор:
I think it's important to mention with the DateTime class that if you're trying to create a system that should store UNIX timestamps in UTC/GMT, and then convert them to a desired custom time-zone when they need to be displayed, using the following code is a good idea:

<?php
date_default_timezone_set
('UTC');
?>

Even if you use something like:

<?php
$date
->setTimezone( new DateTimeZone('UTC') );
?>

... before you store the value, it doesn't seem to work because PHP is already trying to convert it to the default timezone.
2010-06-21 14:58:46
http://php5.kiev.ua/manual/ru/book.datetime.html
Автор:
A very verbose loop.  The construct function for the DateTime class isn't working properly for me but this works.
    <?php 
   
    $date 
"2011/03/20";
   
$date explode("/"$date);
   
   
$time "07:16:17";
   
$time explode(":"$time);
   
   
$tz_string "America/Los_Angeles"// Use one from list of TZ names timezones
   
$tz_object = new DateTimeZone($tz_string);
   
   
$datetime = new DateTime();
   
$datetime->setTimezone($tz_object);
   
$datetime->setDate($date[0], $date[1], $date[2]);
   
$datetime->setTime($time[0], $time[1], $time[2]);
   
    print 
$datetime->format('Y/m/d H:i:s'); // Prints "2011/03/20 07:16:17"
   
   
?>
2011-03-20 12:44:40
http://php5.kiev.ua/manual/ru/book.datetime.html

    Поддержать сайт на родительском проекте КГБ