手机出现非法的时间戳百度文库提交参数非法是怎么回事

PHP手册 - 取得一个日期的 Unix 时间戳
在线手册:&
参数可以从右向左省略,任何省略的参数会被设置成本地日期和时间的当前值。
&?phpecho&date("M-d-Y",&mktime(0,&0,&0,&12,&32,&1997));echo&date("M-d-Y",&mktime(0,&0,&0,&13,&1,&1997));echo&date("M-d-Y",&mktime(0,&0,&0,&1,&1,&1998));echo&date("M-d-Y",&mktime(0,&0,&0,&1,&1,&98));?&
Example #2 下个月的最后一天
任何给定月份的最后一天都可以被表示为下个月的第 &0&
天,而不是 -1 天。下面两个例子都会产生字符串
&The last day in Feb 2000 is: 29&。
&?php$lastday&=&mktime(0,&0,&0,&3,&0,&2000);echo&strftime("Last&day&in&Feb&2000&is:&%d",&$lastday);$lastday&=&mktime(0,&0,&0,&4,&-31,&2000);echo&strftime("Last&day&in&Feb&2000&is:&%d",&$lastday);?&
在线手册:&
PHP手册 - N: 取得一个日期的 Unix 时间戳
for tothpeter at fbi dot hu
07, 08 and 09 are interpreted as octal values. See
08 and 09 in octal is error.
If you mean decimal numbers, you must write 7,8 and 9. Not 07, 08 and 09.
using on mac with MAMP
$a=mktime(0,0,0,07,01,2012);
echo date('Y-m-d',$a); //
$a=mktime(0,0,0,08,01,2012);
echo date('Y-m-d',$a); //
$a=mktime(0,0,0,09,01,2012);
echo date('Y-m-d',$a); //
$a=mktime(0,0,0,10,01,2012);
echo date('Y-m-d',$a); //
$a=mktime(0,0,0,07,31,2012);
echo date('Y-m-d',$a); //
There's a mistake in my post below. This is the correct code:
$lastDay = date("d",mktime (0,0,0,9+1,0,$year));
&$firstWeekDay = @date("w", @mktime(0,0,0,9,1,2012));
&$weekNumber = ($lastDay == 30 && $firstWeekDay == 6 || $lastDay == 31 && $firstWeekDay &= 5) ? 6 : (($lastDay == "28" && $firstWeekDay == "0") ? 4 : 5);
I'm sorry.
Number of the weeks in a determined month
$lastDay = date("d",mktime (0,0,0,$mon+1,0,$year));
$firstWeekDay = @date("w", @mktime(0,0,0,9,1,2012));
$weekNumber = ($lastDay == 30 && $firstWeekDay == 6) || ($lastDay == 31 && $firstWeekDay &= 5) ? 6 : ($lastDay == "28" && $firstWeekDay == "0") ? 4 : 5;
The first date in array is incorrect, to fix it:
function dates_range($date1, $date2)
&&& if ($date1&$date2)
&& & && $dates_range[]=$date1;
&& & && $date1=strtotime($date1);
&& & && $date2=strtotime($date2);
&& & && while ($date1!=$date2)
&& & & & && $date1=mktime(0, 0, 0, date("m", $date1), date("d", $date1)+1, date("Y", $date1));
&& & & & && $dates_range[]=date('Y-m-d', $date1);
&&& return $dates_
Function to generate array of dates between two dates (date range array)
function dates_range($date1, $date2)
&& if ($date1&$date2)
&& & & $dates_range[]=$date1;
&& & & $date1=strtotime($date1);
&& & & $date2=strtotime($date2);
&& & & while ($date1!=$date2)
&& & & & & $date1=mktime(0, 0, 0, date("m", $date1), date("d", $date1)+1, date("Y", $date1));
&& & & & & $dates_range[]=date('Y-m-d', $date1);
&& return $dates_range;
echo '&pre&';
print_r(dates_range('', ''));
echo '&/pre&';
[EDIT BY danbrown AT php DOT net: Contains a bugfix submitted by (carlosbuz2 AT gmail DOT com) on 04-MAR-2011, with the following note: The first date in array is incorrect.]
warning: mktime expects parameter 1 to be long
It could be a string that is given to mktime, so it has to be converted into an int.
mktime((int)$h, (int)$m, (int)$s, (int)$m, (int)$d, (int)$y);
One of the many problems with Daylight Saving Time / Summer Time is the ambiguity when a specified local time value can refer to two different actual times!& This happens when the local time value is within the relapse range caused by the clocks being set back to proper time.& (eg. if the DST/ST bias is +1 hour, and DST/ST terminates at 02:00 local time, a local time value of 01:30 occurs twice in the same day!)
Because the mktime() function only returns one value, it silently chooses whether to return the time-stamp for the first iteration or the second iteration of a specified local time within this critical range.
To get both possible time-stamps for a local time, compatible with any system locale, time zone, and applicable DST/ST rules, the following function can be used:
&?php function LocalToUT($LocalYear, $LocalMonth, $LocalMonthDay, $LocalHour24, $LocalMinute, $LocalSecond) {
$UTValue = mktime($LocalHour24, $LocalMinute, $LocalSecond, $LocalMonth, $LocalMonthDay, $LocalYear);
&&& $ReturnData = array('initial' =& $UTValue, 'relapse' =& $UTValue);
&&& $Bias = $UTValue - mktime($LocalHour24, $LocalMinute, $LocalSecond, $LocalMonth, $LocalMonthDay - 1, $LocalYear) - 86400;& & if ($Bias == 0) {& & $Bias = mktime($LocalHour24, $LocalMinute, $LocalSecond, $LocalMonth, $LocalMonthDay + 1, $LocalYear) - $UTValue - 86400;& & }
&&& if ($Bias & 0) {& & if (date('Z', $UTValue) !== date('Z', $UTValue + $Bias)) {& & $ReturnData['relapse'] = $UTValue + $Bias;
&& & && if (date('Z', $UTValue - $Bias) !== date('Z', $UTValue)) {& & $ReturnData['initial'] = $UTValue - $Bias;
&& & && }& & return $ReturnData;
Do not be confused by th The interleaved PHP-comment and HTML-comment delimiters prevent PHP code containing "&" from appearing as literal text when viewing or editing an HTML file with embedded PHP code.
I've had this query for an event organizer.
&& $query = "SELECT MAX(dt_atfrom) FROM tb_date LIMIT 1";
&& $raw_data = mysql_query($query);
&& $maxdate = mysql_result($raw_data,0,0);
It would fetch the 'bigger' date of all of those events.
The problem started when having events in the future.
I'll start to explain.
Imagine you're in '2010' and the biggest event date is ''.
$maxdate will return me the correct data '', yet since I was only needing the year i passed this date through the following function:
&& maxyear = date("Y", mktime($maxdate));
mktime was returning me the actual year for every future event instead of the future year.
I ended up doing the following:
&& $maxyear = substr($maxdate,0,4);
Probably there's a better solution...
I was using the following to get a list of month names.
for ($i=1; $i&13; $i++) {
& echo date('F', mktime(0,0,0,$i) . ",";
Normally this outputs -
January,February,March,April,May,June,July,August,
September,October,November,December
However if today's date is the 31st you get instead:
January,March,March,May,May,July,July,August,October,
October,December,December
Why? Because Feb,Apr,June,Sept, and Nov don't have 31 days!
The fix, add the 5th parameter, don't let the day of month default to today's date:
& echo date('F', mktime(0,0,0,$i,1) . ",";
Proper way to convert Excel dates into PHP-friendly timestamps using mktime():
$days = 39994;
$ts = mktime(0,0,0,1,$days-1,1900);
echo date("m/d/Y",$ts);
Excel uses "number of days since Jan. 1, 1900" to store its dates.& It also treats 1900 as a leap year when it wasn't, thus there is an extra day which must be accounted for in PHP (and the rest of the world).& Subtracting 1 from Excel's number will fix this problem.
How many days have& passed since the beginning of the year.... regardless of what year it is...
$days = floor((time()-mktime(null,null,null,1,0,date("Y")))/86400);
&& & & & &&
echo "$days days have passed";
I couldn't find any correct date differentiate function anywhere so I wrote this one which works correctly. It's fully resistant to all troubles with different day count of the month or leap year.
Input must be two timestamps and output is associative array with year, month, day, hour, minute, second items.
It can be used for exact age or similar issues.
function date_diff($d1, $d2){
if ($d1 & $d2){
&&& $temp = $d2;
&&& $d2 = $d1;
&&& $d1 = $temp;
&&& $temp = $d1; }
& $d1 = date_parse(date("Y-m-d H:i:s",$d1));
& $d2 = date_parse(date("Y-m-d H:i:s",$d2));
& if ($d1['second'] &= $d2['second']){
&&& $diff['second'] = $d1['second'] - $d2['second'];
&&& $d1['minute']--;
&&& $diff['second'] = 60-$d2['second']+$d1['second'];
& if ($d1['minute'] &= $d2['minute']){
&&& $diff['minute'] = $d1['minute'] - $d2['minute'];
&&& $d1['hour']--;
&&& $diff['minute'] = 60-$d2['minute']+$d1['minute'];
& if ($d1['hour'] &= $d2['hour']){
&&& $diff['hour'] = $d1['hour'] - $d2['hour'];
&&& $d1['day']--;
&&& $diff['hour'] = 24-$d2['hour']+$d1['hour'];
& if ($d1['day'] &= $d2['day']){
&&& $diff['day'] = $d1['day'] - $d2['day'];
&&& $d1['month']--;
&&& $diff['day'] = date("t",$temp)-$d2['day']+$d1['day'];
& if ($d1['month'] &= $d2['month']){
&&& $diff['month'] = $d1['month'] - $d2['month'];
&&& $d1['year']--;
&&& $diff['month'] = 12-$d2['month']+$d1['month'];
& $diff['year'] = $d1['year'] - $d2['year'];
& return $diff;& &
$born_date = mktime(6,30,0,7,24,2008);
$date_diff_array = date_diff($born_date, time());
print_r($date_diff_array);
to ADD or SUBSTRACT times NOTE that if you dont specify the UTC zone your result is the difference +- your server UTC delay.
if you are ina utc/GMT +1
$hours_diff = strtotime("20:00:00")-strtotime("19:00:00");
echo& date('h:i', $hours_diff)." Hours";
it shows: 02:00 Hours
but if you use a default UTC time:
date_default_timezone_set('UTC');
$hours_diff = strtotime("20:00:00")-strtotime("19:00:00");
echo "&br&". date('h:i', $hours_diff);
it shows: 01:00 Hours.
How to get the first and last dates of the last quarter - useful for things like tax return dates etc.& by Justin
function getLastQuarter() {
&&& $year = date("Y",mktime());
&&& $month = date("m",mktime());
&&& $startmth = $month - 3 - (($month-1) % 3 );
&&& if ($startmth == -2) {
&& & && $startmth+=12;
&& & && $year-=1;
&&& $endmth = $startmth+2;
&&& $last_quarter['start'] = mktime(0,0,0,$startmth,1,$year);
&&& $last_quarter['end'] = mktime(0,0,0,$endmth,date("t",mktime(0,0,0,$endmth,1,$year)),$year);
&&& return $last_quarter;& &
echo "First day of last quarter was : " . date("d-M-Y",$lastquarter['start']) . "\n";
echo "Last day of last quarter was : " . date("d-M-Y",$lastquarter['end']) . "\n";
Convert timestamp to time();
function wp_mktime($_timestamp = ''){
&&& if($_timestamp){
&& & && $_split_datehour = explode(' ',$_timestamp);
&& & && $_split_data = explode("-", $_split_datehour[0]);
&& & && $_split_hour = explode(":", $_split_datehour[1]);
&& & && return mktime ($_split_hour[0], $_split_hour[1], $_split_hour[2], $_split_data[1], $_split_data[2], $_split_data[0]);
[NOTE BY danbrown AT php DOT net: See also ()]
With combination of mktime and getDate and date() you can add hours / seconds / days / months / years to ANY timestamp. Use strtotime() function to convert any type of dates to timestamp
&&& public function addMonthToDate($timeStamp, $totalMonths=1){
&& & && $thePHPDate = getdate($timeStamp); $thePHPDate['mon'] = $thePHPDate['mon']+$totalMonths; $timeStamp = mktime($thePHPDate['hours'], $thePHPDate['minutes'], $thePHPDate['seconds'], $thePHPDate['mon'], $thePHPDate['mday'], $thePHPDate['year']); return $timeStamp;
&&& public function addDayToDate($timeStamp, $totalDays=1){
&& & && $thePHPDate = getdate($timeStamp);
&& & && $thePHPDate['mday'] = $thePHPDate['mday']+$totalDays;
&& & && $timeStamp = mktime($thePHPDate['hours'], $thePHPDate['minutes'], $thePHPDate['seconds'], $thePHPDate['mon'], $thePHPDate['mday'], $thePHPDate['year']);
&& & && return $timeStamp;
&&& public function addYearToDate($timeStamp, $totalYears=1){
&& & && $thePHPDate = getdate($timeStamp);
&& & && $thePHPDate['year'] = $thePHPDate['year']+$totalYears;
&& & && $timeStamp = mktime($thePHPDate['hours'], $thePHPDate['minutes'], $thePHPDate['seconds'], $thePHPDate['mon'], $thePHPDate['mday'], $thePHPDate['year']);
&& & && return $timeStamp;
Add (and subtract) unixtime:
function utime_add($unixtime, $hr=0, $min=0, $sec=0, $mon=0, $day=0, $yr=0) {
& $dt = localtime($unixtime, true);
& $unixnewtime = mktime(
&& && $dt['tm_hour']+$hr, $dt['tm_min']+$min, $dt['tm_sec']+$sec,
&& && $dt['tm_mon']+1+$mon, $dt['tm_mday']+$day, $dt['tm_year']+1900+$yr);
& return $unixnewtime;
Days until Christmas:
&& & $time = mktime(0, 0, 0, 12, 25, 2008, 1) - time();
&& & $days = floor($time/86400);
&& & $hours = floor(($time-($days*86400))/3600);
&& & $mins = floor (($time-($days*86400)-($hours*3600))/60);
&& & $secs = floor ($time-($days*86400)-($hours*3600)-($mins*60));
&& & $tsecs = $time;
&& & $thours = round($time/3600);
&& & if ($tsecs &= 600) {
&& && echo '&html& &head& &title& ' . $tsecs . ' seconds left until 12am Christmas Day &/title& &meta http-equiv="refresh" content="1;url=""&&/head& &body&&span
style="font-size:10pt"&Christmas day in ' . $days . ' days ' . $hours . ' hours ' .& $mins . ' mins ' . $secs . ' seconds!&br&&br&(There are ' . $tsecs . ' seconds in
total)&/span&&/body&&/html&';
&& & } else {
&& && echo '&html& &head& &title& ' . $thours . ' hours left until 12am Christmas Day &/title& &meta http-equiv="refresh" content="10;url=""&&/head& &body&&span
style="font-size:10pt"&Christmas day in ' . $days . ' days ' . $hours . ' hours ' .& $mins . ' mins ' . $secs . ' seconds!&br&&br&(There are ' . number_format($thours)
. ' hours in total and ' . number_format($tsecs) . ' seconds in total)&/span&&/body&&/html&';
Do remember that, counter-intuitively enough, the arguments for month and day are inversed (or middle-endian). A common mistake for Europeans seems to be to feed the date arguments in the expected order (big endian or little endian).
It's clear to see where this weird order comes from (even with the date being big endian the order for all arguments would still be mixed - it's obviously based on the American date format with the time "prefixed" to allow an easier shorthand) and why this wasn't changed (passing the values in the wrong order produces a valid, though unexpected, result in most cases), but it continues to be a source of confusion for me whenever I come back to PHP from other languages or libraries.
function getChgWinDate($dt){
&$y=substr($dt,0,4);
&for($i=31;$i&20;$i--){
& $ts=mktime(3,0,0,10,$i,$y);
& $dy=date('D',$ts);
& if($dy=='Sun') return($y.'/10/'.$i.' 03:00:00');
function getChgSumDate($dt){
&$y=substr($dt,0,4);
&for($i=31;$i&20;$i--){
& $ts=mktime(2,0,0,10,$i,$y);
& $dy=date('D',$ts);
& if($dy=='Sun') return($y.'/03/'.$i.' 02:00:00');
function isSummerDate($dt){
&$b1=getChgWinDate($dt);
&$b2=getChgSumDate($dt);
&if($dt&=$b2&&$dt&$b1) return(true);
&return(false);
function isWinterDate($dt){
&return(!isSummerDate($dt));
$dt = ' 03:15:16';
if( isSummerDate($dt) ){
&echo $dt . " is summer hour in france";
&echo $dt . " is winter hour in france";
here simple sample for timestamps.(using malaysia GMT 8)
$mkendtimep=mktime(date("H")+8, date("i"), date("s"), date("m"), date("d"), date("Y"));
$todaydate=date("(d/m/y) H:i:s", $mkendtimep);
if (date("l")=="Monday") { $mday=M } else
&&& if (date("l")=="Tuesday") { $mday=T } else
&&& if (date("l")=="Wednesday") { $mday=W } else
&&& if (date("l")=="Thursday") { $mday=T } else
&&& if (date("l")=="Friday") { $mday=F } else
&&& if (date("l")=="Saturday") { $mday=S } else
&&& if (date("l")=="Sunday"){& $mday=S }
$realtime="$mday$todaydate (GMT +8)";
..hope it will help you out....
caculate days between two date
& $_endDate = mktime(0,0,0,11,10,2008);
& $_beginDate = mktime(13,26,26,05,31,2007);
& $timestamp_diff= $_endDate-$_beginDate +1 ;
& $days_diff = $timestamp_diff/86400;
When calling mktime(), be sure that you use values without leading zeros.& The date comes out wrong in the following example:
$endts = mktime(12, 00, 00, 12, 08, 2008, 0);
(note the 08 instead of just 8)
C's scanf() has a format specification where leading 0's can indicate an octal value - perhaps this is related?
zola at zolaweb:
Your expression date('U', strtotime($mydate)) evaluates to strtotime($mydate). Converting to a UNIX timestamp is what strtotime() does.
Here is what I use to calculate age. It took me 30 minutes to write and it's quite accurate. What it has special is that it's calculating the number of days a year has (float number), by testing if a year is a leap one or not. This number is used to compute the age.
function get_age($date_start, $date_end) {
&&& $t_lived = get_timestamp($date_end) - get_timestamp($date_start);
&&& $seconds_one_year = get_days_per_year($date_start, $date_end) * 24 * 60 * 60;
&&& $age = array();
&&& $age['years_exact'] = $t_lived / $seconds_one_year;
&&& $age['years'] = floor($t_lived / $seconds_one_year);
&&& $seconds_remaining = $t_lived % $seconds_one_year;
&&& $age['days'] = round($seconds_remaining / (24 * 60 * 60));
&&& return $age;
function get_timestamp($date) {
&&& list($y, $m, $d) = explode('-', $date);
&&& return mktime(0, 0, 0, $m, $d, $y);
function get_days_per_year($date_start, $date_end) {
&&& list($y1) = explode('-', $date_start);
&&& list($y2) = explode('-', $date_end);
&&& $years_days = array();
&&& for($y = $y1; $y &= $y2; $y++) {
&& & && $years_days[] = date('L', mktime(0, 0, 0, 1, 1, $y)) ? 366 : 365;
&&& return round(array_sum($years_days) / count($years_days), 2);
$date_birth = '';
$date_now = date('Y-m-d');
$age = get_age($date_birth, $date_now);
echo '&pre&';
print_r($age);
echo '&/pre&';
It will display something like this:
&&& [years_exact] =& 28.
&&& [years] =& 28
&&& [days] =& 355
If you want to increment the day based on a variable when using a loop you can use this when you submit a form
1. Establish a start date and end date in two different variables
2. Get the number of days between a date
$ndays = (strtotime($_POST['edate']) - strtotime($_POST['sdate'])) / (60 * 60 * 24);
Then here is the string you slip in your loop
$nextday& = date('Y-m-d', mktime(0, 0, 0, date("m", strtotime($_POST['sdate']))& , date("d", strtotime($_POST['sdate']))+ $count, date("Y", strtotime($_POST['sdate']))));
$count is incremented by the loop.
It seems mktime() doesn't return negative timestamps on Linux systems with a version of glibc &= 2.3.3.
Just a small thing to think about if you are only trying to pull the month out using mktime and date.& Make sure you place a 1 into day field.& Otherwise you will get incorrect dates when a month is followed by a month with less days when the day of the current month is higher then the max day of the month you are trying to find.. (Such as today being Jan 30th and trying to find the month Feb.)
The maximum possible date accepted by mktime() and gmmktime() is dependent on the current location time zone.
For example, the 32-bit timestamp overflow occurs at T03:14:08+0000Z.& But if you're in a UTC -0500 time zone (such as EST in North America), the maximum accepted time before overflow (for older PHP versions on Windows) is T22:14:07-0500Z, regardless of whether you're passing it to mktime() or gmmktime().
NB: one 'gotcha' with the implementation of mktime()'s parameters:
for( $i = 1 ;& $i &= 12 ; $i++ )
&&& echo "Month '$i' is: " . date( "F" , mktime( 0 , 0 , 0 , $i ) ) . "\n";
Will output:
Month '1' is: January
Month '2' is: March
Month '3' is: March
Month '4' is: May
Month '5' is: May
Month '6' is: July
Month '7' is: July
Month '8' is: August
Month '9' is: October
Month '10' is: October
Month '11' is: December
Month '12' is: December
on the 31st day of every month.
Why? Because the 5th parameter "day" defaults to "right now," which will not work reliably for days after the 28th.
To make sure this doesn't happen, specify the first day of the month:
mktime( 0 , 0 , 0 , $i , 1 )
Finding out the number of days in a given month and year, accounting for leap years when February has more than 28 days.
function days_in_month($year, $month) {
&&& return( date( "t", mktime( 0, 0, 0, $month, 1, $year) ) );
Hope it helps a soul out there.
It may be useful to note that no E_WARNINGS or E_NOTICES are give if you specify a date &1901 or &2038 on systems where time_t is a 32bit signed integer.
If a date is specified outside of the allowed range you may get some unexpected results as no timestamp will be returned.
You cannot simply subtract or add month VARs using mktime to obtain previous or next months as suggested in previous user comments (at least not with a DD & 28 anyway).
If the date is 03-31-2007, the following yeilds March as a previous month. Not what you wanted.
$dateMinusOneMonth = mktime(0, 0, 0, (3-1), 31,& 2007 );
$lastmonth = date("n | F", $dateMinusOneMonth);
echo $lastmonth;& & ?&
mktime correctly gives you back the 3rd of March if you subtract 1 month from March 31 (there are only 28 days in Feb 07).
If you are just looking to do month and year arithmetic using mktime, you can use general days like 1 or 28 to do stuff like this:
$d_daysinmonth = date('t', mktime(0,0,0,$myMonth,1,$myYear));& && $d_year = date('Y', mktime(0,0,0,$myMonth,1,$myYear));& & & & $d_isleapyear = date('L', mktime(0,0,0,$myMonth,1,$myYear));& & $d_firstdow = date('w', mktime(0,0,0,$myMonth,'1',$myYear));& && $d_firstname = date('l', mktime(0,0,0,$myMonth,'1',$myYear));& && $d_month = date('n', mktime(0,0,0,$myMonth,28,$myYear));& & & && $d_monthname = date('F', mktime(0,0,0,$myMonth,28,$myYear));& & & && $d_month_previous = date('n', mktime(0,0,0,($myMonth-1),28,$myYear));& & & && $d_monthname_previous = date('F', mktime(0,0,0,($myMonth-1),28,$myYear));& && $d_month_next = date('n', mktime(0,0,0,($myMonth+1),28,$myYear));& & & && $d_monthname_next = date('F', mktime(0,0,0,($myMonth+1),28,$myYear));& & & && $d_year_previous = date('Y', mktime(0,0,0,$myMonth,28,($myYear-1)));& & & & $d_year_next = date('Y', mktime(0,0,0,$myMonth,28,($myYear+1)));& & & & $d_weeksleft = (52 - $d_weekofyear);& & & & & & & & & && $d_daysinyear = $d_isleapyear ? 366 : 365;& & & & & & & & $d_daysleft = ($d_daysinyear - $d_dayofyear);& & & & & & & & ?&
There are several warnings here about using mktime() to determine a date difference because of daylight savings time. However, nobody seems to have mentioned the other obvious problem, which is leap years.
Leap years mean that any effort to use mktime() and time() to determine the age (positive or negative) of some timestamp in years will be flawed. There are some years that are 366 days long, therefore you cannot say that there is a set number of seconds per year.
Timestamps are good for determining *real* time, which is not the same thing as *human calendar* time. The Gregorian calendar is only an approximation of real time, which is tweaked with daylight savings time and leap years to make it conform more to humans' expectations of how time should or ought to work. Timestamps are not tweaked and therefore are the only authoritative way of recording in computers a proper order of succession of events, but they cannot be integrated with a Gregorian system unless you take both leap years and DST into account. Otherwise, you may get the wrong number of years when you are approaching a value of exactly X years.
As for PHP, you could still use timestamps as a way of determining age if you took into account not only DST but also whether or not each year is a leap year and adjusted your calculations accordingly. However, this could become messy and inefficient.
There is an alternative approach to calculating days given the day, month and year of the dates to be compared. Compare the years first, and then compare the month and day - if the month and day have already passed (or, if you like, if they match the current month and day), then add 1 to the total for the years.
This solution works because it stays within the Gregorian system and doesn't venture into the world of timestamps.
There is also the issue of leap seconds, but this will only arise if you literally need to get the *exact* age in seconds. In that case, of course, you would also need to verify that your timestamps are exactly correct and are not delayed by script processing time, plus you would need to determine whether your system conforms to UTC, etc. I expect this will hardly be an issue for anybody using PHP, however if you are interested there is an article on this issue on Wikipedia:
There are several notes for mktime which use the number 86400 to differentiate two days. However this technique may pose a problem in case there is a day where the hour change between the two dates to compare.
Consequently, if you want the timestamp difference between the day where the hour change and the next day, it will not be equals to 86400 but either 82800 in case its the winter change of hour day or 90000 for the summer change of hour day.
For example in 2006 :
echo mktime(0,0,0,10,29,2006) - mktime(0,0,0,10,30,2006); ?&
Negative timestamps give problem also using linux as guest operating system inside WMvare with Windows host operating system.
If the month is greater than 12, it goes into the next year. If it is less than 1, it goes into the previous year. Generally, it behaves as you'd expect it to :-)
print date ("F j, Y", mktime (0,0,0,13,1,2004));
print date ("F j, Y", mktime (0,0,0,0,1,2004));
print date ("F j, Y", mktime (0,0,0,14,1,2004));
print date ("F j, Y", mktime (0,0,0,-1,1,2004));
Under Windows, mktime goes until
(03:14:07 ...)
Consider skipping months with mktime().
$nextmonth = date("M",mktime(0,0,0,date("n")+1,date("j"),date("Y")));
On any day in Januari you expect to get Feb, right?
But on January 30th you'll get Mar. It will try Feb 30th, which doesn't exist, and skips another month. Therefore in this case present a day value that will certainly be legal in any month, like day "1".
This will give you next month on any day of the year:
$nextmonth = date("M",mktime(0,0,0,date("n")+1,1,date("Y")));
In the above example it should ne boted that if you try to calculate the command at midnight on the 28/04/2004 you will get an erroneous response. This has been driving me to distraction.
$myTime = mktime( 0, 0, 0, 3, 28, 2004);
Solution I found was to create the time at 3am well after the 2am daylight savings problem, viz:
$myTime = mktime( 3, 0, 0, 3, 28, 2004);
Not sure if this is documented anywhere.
I think it is important to note that the timestamp returned is based upon the number of seconds from the epoch GMT, and then modified by the time zone settings on the server.
mktime(0,0,0,1,1,1970) will not always return 0.& For example with the US eastern time zone (GMT-5) will return 18000 (5 hours past the epoch) and the same function with the time zone set to the US pacific time zone (GMT-8) will return 28800 (8 hours past the epoch).
In an instance where you want time zone independence, you should use the function gmmktime()
With regard to Example 1 and using mktime to correct out-of-range input.
It should be noted that mktime will implement day light saving amends. Consider the following:
print(date("d/m/Y H:i:s",mktime(0,0,0,3,(27 + 1),2004)));
OUTPUT "28/03/:00"
print(date("d/m/Y H:i:s",(mktime(0,0,0,3,27,2004) + (((1 * 24) * 60) * 60))));
OUTPUT "28/03/:00"
Dependent on your requirements this may or may be desirable}

我要回帖

更多关于 下载参数含有非法路径 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信