Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

gmdate

(PHP 3, PHP 4, PHP 5)

gmdate -- Форматирует дату/время по Гринвичу

Описание

string gmdate ( string format [, int timestamp] )

Эта функция идентична функции date() за исключением того, что возвращает время по Гринвичу (GMT). Например, в Финляндии (GMT +0200), первая строка в следующем примере выведет "Jan 01 1998 00:00:00", а вторая - "Dec 31 1997 22:00:00".

Пример 1. gmdate() example

<?php
echo date("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 1998));
echo
gmdate("M d Y H:i:s", mktime(0, 0, 0, 1, 1, 1998));
?>

Замечание: В семействе ОС Microsoft Windows системные библиотеки, реализующие эту функцию, содержат ошибки, поэтому функция gmdate() на этих системах не поддерживает отрицательные значения аргумента timestamp. Для более подробной информации, см. сообщения об ошибках: #22620, #22457, и #14391.

В операционных системах Unix/Linux эта проблема не возникает, так как системные библиотеки в этих системах реализованы корректно.

PHP не может исправить ошибки в системных библиотеках. Для решения этой и подобных проблем обращайтесь к производителю операционной системы.

См. также описание функций date(), mktime(), gmmktime() и strftime().



gmmktime> <gettimeofday
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
gmdate
jhechtf at gmail dot com
24-Jul-2007 08:19
My function for something like this is like so:
<?php
function actual_time($format,$offset,$timestamp){
  
//Offset is in hours from gmt, including a - sign if applicable.
   //So lets turn offset into seconds
  
$offset = $offset*60*60;
  
$timestamp = $timestamp + $offset;
   
//Remember, adding a negative is still subtraction ;)
  
return gmdate($format,$timestamp);
}
?>
It's always worked fine for me.
Blazeme
15-Jun-2007 03:25
Here, I wrote a function (from code above) for easy time zone
settings.
Regards.
<?php
function datum($datum=true) {
$sign = "+"; // Whichever direction from GMT to your timezone. + or -
$h = "1"; // offset for time (hours)
$dst = true; // true - use dst ; false - don't

if ($dst==true) {
   
$daylight_saving = date('I');
    if (
$daylight_saving){
        if (
$sign == "-"){ $h=$h-1;  }
        else {
$h=$h+1; }
    }
}
$hm = $h * 60;
$ms = $hm * 60;
if (
$sign == "-"){ $timestamp = time()-($ms); }
else {
$timestamp = time()+($ms); }
$gmdate = gmdate("m.d.Y. g:i A", $timestamp);
if(
$datum==true) {
return
$gmdate;
}
else {
return
$timestamp;
}

}
?>

If you set first argument to true, it'll return formated date.
If false, will return $timestamp.
Enjoy!
code at ashleyhunt dot co dot uk
27-Mar-2007 07:53
I wanted to get the time past from two MySQL dates and came up with this code that does the trick.
Supply a start date, end date and optional output date/time format the default is in seconds but will expand from SS to MM:SS and then to HH:MM:SS automatically, you may wish to force a date format that will not be dynamic (site layout etc). See examples below, also see function date() for more date format options.

<?
function calculate_time_past($start_time, $end_time, $format = "s") {
    $time_span = strtotime($end_time) - strtotime($start_time);
    if ($format == "s") { // is default format so dynamically calculate date format
        if ($time_span > 60) { $format = "i:s"; }
        if ($time_span > 3600) { $format = "H:i:s"; }
    }
    return gmdate($format, $time_span);
}

$start_time = "2007-03-28 00:50:14"; // 00:50:14 will work on its own
$end_time = "2007-03-28 00:52:59"; // 00:52:59 will also work instead

echo calculate_time_past($start_time, $end_time) . "<br />"; // will output 02:45
echo calculate_time_past($start_time, $end_time, "H:i:s"); // will output 00:02:45 when format is overridden
?>

I hope it of use.
Regards,
Ashley
tobias at dcode dot se
22-Mar-2007 04:44
Thanks to Kogik's contribution above I was able to find a way for exec a script thru cron a specific time (12:30, having the crontab on a US server and the actual script on a Swedish server.

$timezone = 1; // (GMT 01:00) Amsterdam, Berlin, Bern, Rom, Stockholm, Wien
$now = preg_replace('/\d{1}$/', 0, gmdate("H:i", time()+3600*($timezone+date("I"))));
if ( $now == '12:30' ) echo 'ok to run..';
derek at d3swimming dot com
13-Nov-2006 08:30
I don't really know what I'm doing, so I just stole various parts of this from other people around here and did a little improvising on my own.  Hope this is helpful to somebody.

This script allows you to insert just three variables: direction from GMT to your timezone ($sign), number of hours to your timezone ($h), and whether or not you have daylight savings time ($dst).  The rest, including daylight savings time, will take care of itself (unless I don't know what I'm doing!).

<?php
// Get info about time zone relationship to GMT at: http://wwp.greenwichmeantime.com/

// SELECT TIME ZONE
$sign = "-"; // Whichever direction from GMT to your timezone.
$h = "8"; // Hour for time zone goes here e.g. +8 or -4, just remove the + or -
$dst = "true"; // Just insert "true" if your location uses daylight savings time or "false" if it does not

// DETECT AND ADJUST FOR DAYLIGHT SAVINGS TIME
if ($dst) {
   
$daylight_saving = date('I');
    if (
$daylight_saving){
        if (
$sign == "-"){ $h=$h-1;  }
        else {
$h=$h+1; }
    }
}

// FIND DIFFERENCE FROM GMT
$hm = $h * 60;
$ms = $hm * 60;

// SET CURRENT TIME
if ($sign == "-"){ $timestamp = time()-($ms); }
else {
$timestamp = time()+($ms); }

// SAMPLE OUTPUT
$gmdate = gmdate("m/d/Y g:i:s A", $timestamp);

echo
"Your current time now is :  $gmdate . ";
?>
ttech5593 at gmail dot com
28-Mar-2006 02:53
For me most of the examples here needed the + or - seconds to set the time zone. I wanted a faster way to get the time zone in seconds. So I created this :
<?php
$h
= "3";// Hour for time zone goes here e.g. +7 or -4, just remove the + or -
$hm = $h * 60;
$ms = $hm * 60;
$gmdate = gmdate("m/d/Y g:i:s A", time()-($ms)); // the "-" can be switched to a plus if that's what your time zone is.
echo "Your current time now is :  $gmdate . ";
?>
It works. Hope it helps.
gefiltefishee at comcast dot net
11-Mar-2006 01:54
I was struggling with how to get my browser to output MY local time using gmdate().

I figured it out and here's what you do (ASSUMING THE SERVER IS ON GMT, If not, just echo a generic gmdate() without timezone setting and calculate the number of hours ahead or behind you are of that time, convert it to seconds and add [for ahead] or subtract [for behind] that value to time() ):

NOTE: these are US times [setlocale(LC_TIME, 'en_US')]

for Central Time (7 hours behind GMT):
gmdate("format", time()-(25200));

For Pacific Time (9 hours behind GMT):
gmdate("format", time()-(32400));

REMEMBER - THE VALUES ABOVE ASSUME THE SERVER IS ON GMT

I used the following gmdate() format:
"l, F jS, Y  g:i a"
but you can use what you like ;)

Hope this helps!
fernandobassani at gmail dot com
28-Dec-2005 05:35
If you have the same application running in different countries, you may have some troubles getting the local time..
In my case, I was having troubles with a clock created with Macromedia Flash... the time shown by the clock was supposed to be set up by the server, passing the timestamp. When I moved the file to another country, I got a wrong time...
You can use the timezone offset ( date("Z") ) to handle this kind of thing...

<?php
$timestamp
= time()+date("Z");
echo
gmdate("Y/m/d H:i:s",$timestamp);
?>
Sir Derek G
24-Nov-2005 11:00
Here's a nifty little function that returns a random timestamp between two dates.

<?
//////////////////////////////////////////////////////////
// Return a random timestamp between two dates (inclusive)
// Example: Tue, 08 Nov 2004 06:47:10 GMT
//
// time - Starting time string
// Valid Examples:
// 10 September 2001
// next Thursday
// last Monday
// now
//
// time2 - Ending time string
function randomTimestamp($time = "" , $time2 = "")
{
    if(!$time) $time = strtotime("10 September 2000");
    if(!$time2) $time2 = strtotime("24 November 2005");
    $timestamp = date(" D, d M Y", rand( settype($time , int) , settype($time2 , int) )); //Must be called once before becoming random, ???
    $timestamp = date(" D, d M Y", rand($time , $time2))." ";//Now it's random
   
    $h = rand(1,23);
    if(strlen($h) == 1 ) $h = "0$h";
    $t = $h.":";
   
    $d = rand(1,29);
    if(strlen($d) == 1 ) $d = "0$d";
    $t .= $d.":";
   
    $s = rand(0,59);
    if(strlen($s) == 1 ) $s = "0$s";
    $t .= $s;
   
    $timestamp .= $t." GMT";
    return $timestamp;
}
?>
faraz at gmail dot com
15-Nov-2005 08:40
Hi i was having a problem in converting a date to any time zone so I wrote my own function which coverts a given date and time to other time zone equivalent

function printByTimeZone($mytime,$mydate,$mytimezone)
{
   
 //assuming timezone in format +05:00 or -4:30

$conTime = $mytime;
$conDate = $mydate;

$tzonehr=substr($mytimezone,1,2);
$tzonesec=substr($mytimezone,4,2);
$sign= substr($mytimezone,0,1);

$timezone=intval($tzonehr)*60 + ( intval($tzonesec)*3600);
Switch($sign)
{
Case
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571