A study of date processing function and Uchome Function_coomon in PHP _php techniques

Source: Internet
Author: User
Tags dateformat echo date
Copy Code code as follows:

<?php echo Time ();
echo Mktime (11,25,0,9,5,2010);
Echo Microtime ();
echo Mktime (0,0,0,1,1,1970);
?>

This output is 1283657290, 1283657100, 0.88533200 1283657290, and-25200 respectively. From the last value, we know that the timestamp returned here is adjusted by the time zone, which is my January 1, 1970 0 o'clock in China, Greenwich is not yet 0, so the time here will be negative and the whole is equal to -8*3600.

Look again
Copy Code code as follows:

<?php
echo Date ("H i l D F", 1283657100);
Echo gmdate ("H i l D F", 1283657100);
Echo strftime ("%hh%m%A%d%b", 1283657100);
The way Strftime () works is no different from date (), except that a percent semicolon must be added before a specially formatted character.
Echo strtotime ("2010-9-5 11:25:00");
Var_dump (GETDATE (Time ()));
?>

The output here is one Sunday September, Sunday September, 11h25 Sunday Sep, 1283657100, Array (11) {
["Seconds"]=>
Int (9)
["Minutes"]=>
Int (39)
["Hours"]=>
Int (11)
["Mday"]=>
Int (5)
["Wday"]=>
Int (0)
["Mon"]=>
Int (9)
["Year"]=>
Int (2010)
["Yday"]=>
Int (247)
["Weekday"]=>
String (6) "Sunday"
["Month"]=>
String (9) "September"
[0]=>
Int (1283657949)
}

Mainly look at the third output, where the output is in China September 5, 2010 11:25 Greenwich Time is how much. The time difference is also to be counted here. And one of the amazing thing about this is that only gmdate doesn't give a warning, and the rest of them have a warning that they can't rely on the system's time zone. Imagine also, because Gmdate calculated only Greenwich time, even if the system time zone is wrong, one plus one minus is normal again.
Copy Code code as follows:

Time formatting
function Sgmdate ($dateformat, $timestamp = ', $format =0) {
Global $_sconfig, $_sglobal;
if (empty ($timestamp)) {
$timestamp = $_sglobal[' timestamp '];
}
$timeoffset = strlen ($_sglobal[' member '] [' timeoffset ']) >0?intval ($_sglobal[' member '] [' Timeoffset ']): Intval ($_ sconfig[' Timeoffset ']);
$result = ';
if ($format) {
$time = $_sglobal[' timestamp ']-$timestamp;
if ($time > 24*3600) {
$result = Gmdate ($dateformat, $timestamp + $timeoffset * 3600);
} elseif ($time > 3600) {
$result = Intval ($time/3600). Lang (' Hour '). Lang (' before ');
} elseif ($time > 60) {
$result = Intval ($time/60). Lang (' minute '). Lang (' before ');
} elseif ($time > 0) {
$result = $time. Lang (' second '). Lang (' before ');
} else {
$result = lang (' Now ');
}
} else {
$result = Gmdate ($dateformat, $timestamp + $timeoffset * 3600);
}
return $result;
}

We look directly at what is in the IF ($format) {}, and first we get the current time of the system and the time I pass in (typically the time in the database, like 2010-9-4 21:00:00). If the time difference is within a day, then direct conclusions such as two hours ago, if it is greater than 1 days, then call Gmdate. I just don't get it here. Why call this weird function instead of the direct date ($timestamp)? What the hell does that mean?
Gmdate

When run in Finland (GMT +0200), the "below prints" is the "I-1998 00:00:00" while the second prints "Dec 31 1997 22:00:00 ".
Copy Code code as follows:

<?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));
?>

In other words, Gmdate is considering the time difference. This will output the standard time format, not a few days ago.
Next, focus on the function in Uchome's Function_common.
Copy Code code as follows:

String-time
function Sstrtotime ($string) {
Global $_sglobal, $_sconfig;
$time = ';
if ($string) {
$time = Strtotime ($string);
if (gmdate (' h:i ', $_sglobal[' timestamp '] + $_sconfig[' timeoffset '] * 3600)!= date (' h:i ', $_sglobal[' timestamp ')) {
$time = $time-$_sconfig[' timeoffset '] * 3600;
}
}
return $time;
}

Although this function is very few, but I am very difficult to see. This is to convert a time string to a timestamp. For example, I entered here is 2010 9 4 21:08, then the $time value is this time to January 1 1970 00:00:00 GMT, here is to consider what jet lag. The value of $_sglobal[' timestamp ' is actually calculated in the same way as $time, but the number may be slightly different. $_sconfig[' Timeoffset ' is in the table in config, which is currently 8. There is a situation where the system's time zone is right or wrong and needs to be tested by $_sconfig[' Timeoffset '. Gmdate passes the incoming timestamp (the timestamp of the place where the program runs) through the system's time zone to get the time at that place in Greenwich. If it is the system time zone is set to the right, then this is just a plus minus, and the following is equal (in fact, here $_sglobal[' timestamp] The value is irrelevant). If it is normal, then do not need to change $time, if it is not normal, then need to reduce. But what the hell does that mean, anyway?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.