php中怎么用date函数java输出int类型值数据,截图所示有错误 怎么解?

php中用date函数获取当前时间有误的解决办法
字体:[ ] 类型:转载 时间:
以下是对php中用date函数获取当前时间有误的解决办法进行了详细的分析介绍,需要的朋友可以过来参考下
初学PHP做网站,想在页面上获得当前时间,学过编程的人都知道用时间函数date(),先用这个函数格式化一个本地时间/日期,先写个测试代码吧,结果输出时间比实际时间少了8小时,这是什么原因呢: 代码如下:&?php &&& echo date('Y-m-d H:i:s'); ?〉输出当前时间: 02:32:17 怪了,实际时间是: 10:32:17
难道是PHP的date()时间不正确 少8个小时? 再看看PHP手册的“例子 1. date() 例子”第一行多了一个 时区设置// 设定要用的默认时区。自 PHP 5.1 可用 date_default_timezone_set('UTC');
原来php5.1.开始,php.ini里加入了date.timezone这个选项,默认情况下是关闭的,也就是显示的时间(无论用什么php命令)都是格林威治标准时间,和北京时间正好差8个小时。
如何设置可以得到正确的PHP时间?
1、修改php.ini。打开php.ini查找date.timezone 去掉前面的分号= 后面加Asia/Shanghai,重启apache服务器即可--缺点就是如果程序 放到别人的服务器上,不能修改php.ini,那就奈何不得了。
2、在程序中添加时间的初始化的语句的 即:“date_default_timezone_set("Asia/Shanghai"); ”这个可以由程序员任意设置,我的推荐。 时区标识符,大陆内地可用的值是:PRC,Asia/Chongqing ,Asia/Shanghai ,Asia/Urumqi (依次为中国,重庆,上海,乌鲁木齐) ,Etc/GMT-8,Asia/Harbin
港台地区可用:Asia/Macao ,Asia/Hong_Kong ,Asia/Taipei (依次为澳门,香港,台北)还有新加坡:Asia/Singapore 这样就可以实现输出的是北京时间了。
更详细的时区代码 时区编码 时区索引码 timezone_identifier 大家可以在官方网站中查到。
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具PHP Date/Time 函数
在线手册:&
在线手册:&
I&needed&a&way&to&display&an&announcement&on&a&shopping&site,&that&would&warn&users&that&orders&placed&between&a&certain&date&range,&would&not&be&shipped&until&after&a&certain&date.I&created&this&simple&date&detection&code&to&display&the&notice&on&certain&pages.&You&can&just&copy&the&code&and&save&it&to&a&file&on&the&site&and&include&it&anywhere&you&need&to&perform&a&function,&or&display&a&notice.&?php/*&&&&Code&to&show&a&message&only&for&a&certain&time&frame.&&&&This&is&a&simple&include&file&that&can&be&used&to&display&a&message&&&&on&any&pages&that&use&it.&&&&Simply&us&a&standard&include&instruction&to&this&file&on&the&page/s&where&&&&&you&want&the&notice&to&appear.&&&&Written&by&Hans&Kiesouw&-&hans&at&wotworx&dot&com*/$start&=&new&DateTime('30-07-2011');&//&DD-MM-YYYY$endDate=&new&DateTime('07-08-2011');&//&DD-MM-YYYY$curdate&=&new&DateTime(date('d-m-Y'));if&($start&&=&$curdate&&&&$curdate&&=&$endDate)&{&&&&/*&&&&The&message&below&will&appear&if&the&current&date&is&between&the&start&and&&&&&endDate&-&used&standards&HTML&to&ensure&that&any&code&will&not&be&&&&&escaped&by&PHP.&You&can&use&any&code&here&to&wish&to&execute&for&the&&&&&date&range.&&&&*/&&&&?&&&&&&&&&&p&&strong&&font&color="#FF0000"&Please&Note:&Any&orders&placed&between&July&30th&and&August&7th&will&only&be&shipped&on&August&8th.&We&apologize&for&any&inconvenience&and&thank&you&for&your&order.&/font&&/strong&&/p&&&?php&&&//&don't&forget&this&last&bit,&it&ends&the&if&statement!}&?&
This&is&an&easily&extendable&and&pretty&way&to&output&human-readable&date&differences&such&as&"1&day&2&hours&ago",&"6&months&ago",&"3&years&7&months&14&days&1&hour&4&minutes&16&seconds"&etc&etc.
Change&"$levels&=&2;"&to&whatever&you&want.&A&value&of&1&will&limit&to&only&one&number&in&the&result&("3&days&ago").&A&value&of&3&would&result&in&up&to&three&("3&days&1&hour&2&minutes&ago")
It&can&be&used&in&the&following&ways:
echo&compare_dates($start_date,$end_date);
echo&compare_dates($end_date,$start_date);
echo&compare_dates($start_date);&//end&date&will&be&assumed&as&time();
&?php
function&compare_dates($date1,&$date2&=&time())
&&&&{
&&&&$blocks&=&array(
&&&&&&&&array('name'=&'year','amount'&&&&=&&&&&60*60*24*365&&&&),
&&&&&&&&array('name'=&'month','amount'&&&&=&&&&&60*60*24*31&&&&),
&&&&&&&&array('name'=&'week','amount'&&&&=&&&&&60*60*24*7&&&&),
&&&&&&&&array('name'=&'day','amount'&&&&=&&&&&60*60*24&&&&),
&&&&&&&&array('name'=&'hour','amount'&&&&=&&&&&60*60&&&&&&&&),
&&&&&&&&array('name'=&'minute','amount'&&&&=&&&&&60&&&&&&&&),
&&&&&&&&array('name'=&'second','amount'&&&&=&&&&&1&&&&&&&&)
&&&&&&&&);
&&&&
&&&&$diff&=&abs($date1-$date2);
&&&&
&&&&$levels&=&2;
&&&&$current_level&=&1;
&&&&$result&=&array();
&&&&foreach($blocks&as&$block)
&&&&&&&&{
&&&&&&&&if&($current_level&&&$levels)&{}
&&&&&&&&if&($diff/$block['amount']&&=&1)
&&&&&&&&&&&&{
&&&&&&&&&&&&$amount&=&floor($diff/$block['amount']);
&&&&&&&&&&&&if&($amount&1)&{$plural='s';}&else&{$plural='';}
&&&&&&&&&&&&$result[]&=&$amount.'&'.$block['name'].$plural;
&&&&&&&&&&&&$diff&-=&$amount*$block['amount'];
&&&&&&&&&&&&$current_level++;
&&&&&&&&&&&&}
&&&&&&&&}
&&&&return&implode('&',$result).'&ago';
&&&&}
?&
[EDIT&BY&danbrown&AT&php&DOT&net:&Contains&a&bugfix&supplied&by&(jorge&AT&dontspam&DOT&com)&on&22-OCT-2009.]
With PHP 5.1 and 5.2 the languages datetime support has changed. Although these functions should guess your local timezone settings, they may fail if using a default configuration in a &pre-5.1 way&, which means setting no timezone for PHP. In the case PHP could not get a timezone it emits a E_STRICT warning. Note that this affects _all_ datetime functions and keep it in mind when porting software from previous versions to 5.1 or later! It may also confuse your error handling (this is the way I noticed that things have changed, since these changes are not documentated _here_).
References:
http://www.php.net/manual/de/migration51.datetime.php
http://www.php.net/manual/de/migration52.datetime.php
&?php
####################################
#&Provide&week&number&and&get&start_timestamp&and&end_timestamp
#####################################
//&this&week&number&will&come&from&the&timeshare&form
&$week&=&51;
$times&=&get_start_and_end_date_from_week($week);
$start_time&=&$times['start_timestamp'];
$end_time&=&$times['end_timestamp'];
function&get_start_and_end_date_from_week&($w)&
{
&&&&$y&=&date("Y",&time());
&&&&$o&=&6;&//&week&starts&from&sunday&by&default
&&&&$days&=&($w&-&1)&*&7&+&$o;
&&&&$firstdayofyear&=&getdate(mktime(0,0,0,1,1,$y));
&&&&if&($firstdayofyear["wday"]&==&0)&$firstdayofyear["wday"]&+=&7;
&&&&#&in&getdate,&Sunday&is&0&instead&of&7
&&&&$firstmonday&=&getdate(mktime(0,0,0,1,1-$firstdayofyear["wday"]+1,$y));
&&&&$calcdate&=&getdate(mktime(0,0,0,$firstmonday["mon"],&$firstmonday["mday"]+$days,$firstmonday["year"]));
&&&&$sday&=&$calcdate["mday"];
&&&&$smonth&=&$calcdate["mon"];
&&&&$syear&=&$calcdate["year"];
&&&&
&&&&&&&&
&&&&$timestamp['start_timestamp']&=&&mktime(0,&0,&0,&$smonth,&$sday,&$syear);
&&&&$timestamp['end_timestamp']&=&&$timestamp['start_timestamp']&+&(60*60*24*7);
&&&&return&$timestamp;
}&&&&#&function&datefromweek
?&
Here's&my&version&of&date&diff&that&handles&leap&years,&DST,&and&deals&with&the&diff&in&terms&of&"human&calculations".&For&example,&this&code&does&not&assume&that&a&year&=&365.25&days,&or&that&a&month&=&30/31&days.&The&results&are&exactly&what&a&human&being&would&determine&as&a&good&result.
/**
&*&this&code&assumes&php&&=&5.1.0.&if&using&&&5.1,&read
&*&php.net/strtotime&and&change&the&condition&for&checking
&*&for&failure&from&strtotime()
&*/
//&$t1,&$t2:&unix&times,&or&strtotime&parseable
//&$precision:&max&number&of&units&to&output
//&$abbr:&if&true,&use&"hr"&instead&of&"hour",&etc.
function&date_diff&($t1,&$t2,&$precision&=&6,&$abbr&=&false)&{
&&&&if&(preg_match('/\D/',&$t1)&&&&($t1&=&strtotime($t1))&===&false)
&&&&&&&&return&false;
&&&&if&(preg_match('/\D/',&$t2)&&&&($t2&=&strtotime($t2))&===&false)
&&&&&&&&return&false;
&&&&if&($t1&&&$t2)
&&&&&&&&list($t1,&$t2)&=&array($t2,&$t1);
&&&&$diffs&=&array(
&&&&&&&&'year'&=&&0,&'month'&=&&0,&'day'&=&&0,
&&&&&&&&'hour'&=&&0,&'minute'&=&&0,&'second'&=&&0,
&&&&);
&&&&$abbrs&=&array(
&&&&&&&&'year'&=&&'yr',&'month'&=&&'mth',&'day'&=&&'day',
&&&&&&&&'hour'&=&&'hr',&'minute'&=&&'min',&'second'&=&&'sec'
&&&&);
&&&&foreach&(array_keys($diffs)&as&$interval)&{
&&&&&&&&while&($t2&&=&($t3&=&strtotime("+1&${interval}",&$t1)))&{
&&&&&&&&&&&&$t1&=&$t3;
&&&&&&&&&&&&++$diffs[$interval];
&&&&&&&&}
&&&&}
&&&&$stack&=&array();
&&&&foreach&($diffs&as&$interval&=&&$num)
&&&&&&&&$stack[]&=&array($num,&($abbr&?&$abbrs[$interval]&:&$interval)&.&($num&!=&1&?&'s'&:&''));
&&&&$ret&=&array();
&&&&while&(count($ret)&&&$precision&&&&($item&=&array_shift($stack))&!==&null)&{
&&&&&&&&if&($item[0]&&&0)
&&&&&&&&&&&&$ret[]&=&"{$item[0]}&{$item[1]}";
&&&&}
&&&&return&implode(',&',&$ret);
}
$t1&=&'Feb&4,&:00';
$t2&=&'Jul&3,&:30';
echo&date_diff($t1,&$t2),&"\n",
&&&&date_diff($t1,&$t2,&3),&"\n",
&&&&date_diff($t1,&$t2,&2,&true),&"\n";
A&better&and&accurate&function&to&calculate&the&difference&between&2&dates.&Takes&leap&years&and&DST&into&consideration.&Accepts&string&date&or&timestamp&as&arguments.&?phpfunction&date_diff($d1,&$d2){&&&&$d1&=&(is_string($d1)&?&strtotime($d1)&:&$d1);&&&&$d2&=&(is_string($d2)&?&strtotime($d2)&:&$d2);&&&&$diff_secs&=&abs($d1&-&$d2);&&&&$base_year&=&min(date("Y",&$d1),&date("Y",&$d2));&&&&$diff&=&mktime(0,&0,&$diff_secs,&1,&1,&$base_year);&&&&return&array(&&&&&&&&"years"&=&&date("Y",&$diff)&-&$base_year,&&&&&&&&"months_total"&=&&(date("Y",&$diff)&-&$base_year)&*&12&+&date("n",&$diff)&-&1,&&&&&&&&"months"&=&&date("n",&$diff)&-&1,&&&&&&&&"days_total"&=&&floor($diff_secs&/&(3600&*&24)),&&&&&&&&"days"&=&&date("j",&$diff)&-&1,&&&&&&&&"hours_total"&=&&floor($diff_secs&/&3600),&&&&&&&&"hours"&=&&date("G",&$diff),&&&&&&&&"minutes_total"&=&&floor($diff_secs&/&60),&&&&&&&&"minutes"&=&&(int)&date("i",&$diff),&&&&&&&&"seconds_total"&=&&$diff_secs,&&&&&&&&"seconds"&=&&(int)&date("s",&$diff)&&&&);}$a&=&date_diff("",&"");echo&"&pre&";print_r($a);echo&"&/pre&";?&This&example&will&output&(if&your&timezone&uses&US&DST):Array(&&&&[years]&=&&0&&&&[months_total]&=&&11&&&&[months]&=&&11&&&&[days_total]&=&&364&&&&[days]&=&&30&&&&[hours_total]&=&&8759&&&&[hours]&=&&23&&&&[minutes_total]&=&&525540&&&&[minutes]&=&&0&&&&[seconds_total]&=&&&&&&[seconds]&=&&0)As&you&can&see,&the&result&is&not&exactly&1&year&(less&1&hour)&since&Nov&1,&2006&is&not&DST&while&Nov&1,&2007&is&DST.
May&be&useful&for&somebody.&This&function&takes&on&daylight&saving&time
&?php
Function&DateDiff($date1,$date2)&{
&&$timedifference=$date2-$date1;
&&$corr=date("I",$date2)-date("I",$date1);
&&$timedifference+=$corr;
&&return&$timedifference;
}
?&
&?php
$d1=mktime(2,0,0,10,28,2007);
$d2=mktime(4,0,0,10,28,2007);
$period=DateDiff($d1,$d2);
printf("&br&%s",date("I&d.m.Y&H:i",$d1));
printf("&br&%u&hour",$period/3600);
printf("&br&%s",date("I&d.m.Y&H:i",$d2));
?&
Getting&2&hour&instead&3.
Not&really&elegant,&but&tells&you,&if&your&installed&timezonedb&is&the&most&recent:
&?php
class&TestDateTimeTimezonedbVersion&extends&PHPUnit_Framework_TestCase
{
&&&&public&function&testTimezonedbIsMostRecent()
&&&&{
&&&&&&&&ini_set(&'date.timezone',&'Europe/Berlin'&);
&&&&&&&&ob_start();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&phpinfo(INFO_MODULES);
&&&&&&&&$info&=&ob_get_contents();&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&ob_end_clean();&
&&&&&&&&$start&=&strpos(&$info,&'Timezone&Database&Version'&)&+&29;
&&&&&&&&$this-&assertTrue(&FALSE&!==&$start,&'Seems&there&is&no&timezone&DB&installed'&);
&&&&&&&&$end&&&=&strpos(&$info,&"\n",&$start&);
&&&&&&&&$installedVersion&=&substr(&$info,&$start,&$end&-&$start&);
&&&&&&&&exec(&'pecl&remote-info&timezonedb',&&$output&);
&&&&&&&&$availableVersion&=&substr(&$output[2],&12&);
&&&&&&&&$this-&assertEquals(&$availableVersion,&$installedVersion,&
&&&&&&&&'The&installed&timezonedb&is&not&actual.&Installed:&'.$installedVersion
&&&&&&&&.'&available:&'.$availableVersion
&&&&&&&&);
&&&&}
}
?&
I&wrote&a&simple&script&to&format&a&duration&in&seconds.&Give&the&function&some&value&in&seconds&and&it&will&return&an&array.&?phpfunction&format_duration($seconds)&{&&&&$periods&=&array(&&&&&&&&'centuries'&=&&,&&&&&&&&'decades'&=&&,&&&&&&&&'years'&=&&,&&&&&&&&'months'&=&&2629743,&&&&&&&&'weeks'&=&&604800,&&&&&&&&'days'&=&&86400,&&&&&&&&'hours'&=&&3600,&&&&&&&&'minutes'&=&&60,&&&&&&&&'seconds'&=&&1&&&&);&&&&$durations&=&array();&&&&foreach&($periods&as&$period&=&&$seconds_in_period)&{&&&&&&&&if&($seconds&&=&$seconds_in_period)&{&&&&&&&&&&&&$durations[$period]&=&floor($seconds&/&$seconds_in_period);&&&&&&&&&&&&$seconds&-=&$durations[$period]&*&$seconds_in_period;&&&&&&&&}&&&&}&&&&&&&&return&$durations;}echo&format_duration(864);/*[minutes]&=&&14[seconds]&=&&24*/echo&format_duration(3600);/*[hours]&=&&1*/echo&format_duration();/*[months]&=&&4[days]&=&&6[hours]&=&&20[minutes]&=&&28[seconds]&=&&59*/?&
I was looking for a solution where I could return the number of days, hours, Minutes and seconds between two entries in a table.
DATE_DIFF is not running on my mysql server as my provider uses mysql version 4.0.25
Solution was to use to days and std time functions to calculate the difference in one call.
The fields stored in the table(report_table) are
time(00:00:00),
date() and record(enum) which tells the app the type of log stored. EG start or end of a report.
(TO_DAYS( `end`.`date` ) - TO_DAYS( `start`.`date` ))
( second( `end`.`time` ) + (minute( `end`.`time` )*60) + (hour( `end`.`time` )*3600)
second( `start`.`time` ) + (minute( `start`.`time` )*60) + (hour( `start`.`time` )*3600))
AS `days` ,
SEC_TO_TIME(
(second( `end`.`time` ) + (minute( `end`.`time` )*60) + (hour( `end`.`time` )*3600) )
(second( `start`.`time` ) + (minute( `start`.`time` )*60) + (hour( `start`.`time` )*3600) )
) AS `hms`,
`start`.`time` as `start`,
`end`.`time`
FROM `report_table` AS `start` , `report_table` AS `end`
AND `start`.`record` = 'Report Begin'
AND `end`.`record` = 'Report End'
If there is no end of report then it will not return a result, as you would expect.
Here&is&my&function&to&count&the&number&days,&weeks,&months,&and&year.&I&tried&it&below&1970&and&it&works.&?phpfunction&datecal($date,$return_value){$date&=&explode("/",&$date);$month_begin&=&$date[0];$month_begin_date&=&$date[1];$year1&=&$date[2];$month_end&=&date("n");$month_end_date&=&date("j");$year2&=&date("Y");$days_old&=&0;$years_old&=&0;$months_old&=&0;if($month_begin==12){&&$month&=&1;&&$year&=&$year1+1;}else{&&$month&=&$month_begin+1;&&$year&=&$year1;}$begin_plus_days&=&cal_days_in_month(CAL_GREGORIAN,&$month_begin,&$year1)&-&$month_begin_date;$end_minus_days&=&cal_days_in_month(CAL_GREGORIAN,&$month_end,&$year2)&-&$month_end_date;while&($year&&=&$year2)&{&&&&&&&&&if($year&==&$year2)&&&&{&&&&&&$days_old&=&$days_old&+&cal_days_in_month(CAL_GREGORIAN,&$month,&$year);&&&&&&&&&&&if($month&&&$month_end)&&&&&&&&{&&&&&&&&&&$months_old&=&$months_old&+&1;&&&&&&&&&&&&&$month&=&$month&+&1;&&&&&&&&}&&&&&&&&&&elseif&($month==$month_end&and&$month_end_date&&=&$month_begin_date)&&&&&&&&&&&&{&&&&&&&&&&$year&=&$year2+1;&&&&&&&&&&&&}&&&&&&else&&&&&&&&{&&&&&&&&&&&&&$year&=&$year2+1;&&&&&&&&&&&&}&&&&}&&&&else&&&&{&&&&&$days_old&=&$days_old&+&cal_days_in_month(CAL_GREGORIAN,&$month,&$year);&&&&&&&&&if&($month&&=&11)&&&&&&&&&&&&&{&&&&&&&&&$month&=&$month&+&1;&&&&&&&&&$months_old&=&$months_old&+&1;&&&&&&&&&&&&&&&&}&&&&&&&&&else&&&&&&&&&&&&{&&&&&&&&&$month&=&1;&&&&&&&&&&$year&=&$year&+&1;&&&&&&&&&$months_old&=&$months_old&+&1;&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&}}$days_old&=&($days_old&+&$begin_plus_days)&-&$end_minus_days;if($return_value&==&"d")&&{&return&$days_old;&}elseif&($return_value&==&"w")&&{&return&intval($days_old/7);&}elseif&($return_value&==&"m")&&{&return&$months_old;&}elseif&($return_value&==&"y")&&{&return&intval($months_old/12);&}}echo&datecal("08/13/1975","m");?&
When&debugging&code&that&stores&date/time&values&in&a&database,&you&may&find&yourself&wanting&to&know&the&date/time&that&corresponds&to&a&given&unix&timestamp,&or&the&timestamp&for&a&given&date&&&time.
The&following&script&will&do&the&conversion&either&way.&&If&you&give&it&a&numeric&timestamp,&it&will&display&the&corresponding&date&and&time.&&If&you&give&it&a&date&and&time&(in&almost&any&standard&format),&it&will&display&the&timestamp.
All&conversions&are&done&for&your&locale/time&zone.
&?php
&&&&&&&&while&(true)
&&&&&&&&{
&&&&&&&&&&&&&&&&//&Read&a&line&from&standard&in.
&&&&&&&&&&&&&&&&echo&"enter&time&to&convert:&";
&&&&&&&&&&&&&&&&$inline&=&fgets(STDIN);
&&&&&&&&&&&&&&&&$inline&=&trim($inline);
&&&&&&&&&&&&&&&&if&($inline&==&""&||&$inline&==&".")
&&&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&//&See&if&the&line&is&a&date.
&&&&&&&&&&&&&&&&$pos&=&strpos($inline,&"/");
&&&&&&&&&&&&&&&&if&($pos&===&false)&{
&&&&&&&&&&&&&&&&&&&&&&&&//&not&a&date,&should&be&an&integer.
&&&&&&&&&&&&&&&&&&&&&&&&$date&=&date("m/d/Y&G:i:s",&$inline);
&&&&&&&&&&&&&&&&&&&&&&&&echo&"int2date:&$inline&-&&$date\n";
&&&&&&&&&&&&&&&&}&else&{
&&&&&&&&&&&&&&&&&&&&&&&&$itime&=&strtotime($inline);
&&&&&&&&&&&&&&&&&&&&&&&&echo&"date2int:&$inline&-&&$itime\n";
&&&&&&&&&&&&&&&&}
&&&&&&&&}
?&
&?php
&&&&&&&//function&like&dateDiff&Microsoft
&&&&&&&//not&error&in&year&Bissesto
&&&&&&&function&dateDiff($interval,$dateTimeBegin,$dateTimeEnd)&{
&&&&&&&&&//Parse&about&any&English&textual&datetime
&&&&&&&&&//$dateTimeBegin,&$dateTimeEnd
&&&&&&&&&$dateTimeBegin=strtotime($dateTimeBegin);
&&&&&&&&&if($dateTimeBegin&===&-1)&{
&&&&&&&&&&&return("..begin&date&Invalid");
&&&&&&&&&}
&&&&&&&&&$dateTimeEnd=strtotime($dateTimeEnd);
&&&&&&&&&if($dateTimeEnd&===&-1)&{
&&&&&&&&&&&return("..end&date&Invalid");
&&&&&&&&&}
&&&&&&&&&$dif=$dateTimeEnd&-&$dateTimeBegin;
&&&&&&&&&switch($interval)&{
&&&&&&&&&&&case&"s"://seconds
&&&&&&&&&&&&&&&return($dif);
&&&&&&&&&&&case&"n"://minutes
&&&&&&&&&&&&&&&return(floor($dif/60));&//60s=1m
&&&&&&&&&&&case&"h"://hours
&&&&&&&&&&&&&&&return(floor($dif/3600));&//3600s=1h
&&&&&&&&&&&case&"d"://days
&&&&&&&&&&&&&&&return(floor($dif/86400));&//86400s=1d
&&&&&&&&&&&case&"ww"://Week
&&&&&&&&&&&&&&&return(floor($dif/604800));&//week=1semana
&&&&&&&&&&&case&"m":&//similar&result&"m"&dateDiff&Microsoft
&&&&&&&&&&&&&&&$monthBegin=(date("Y",$dateTimeBegin)*12)+
&&&&&&&&&&&&&&&&&date("n",$dateTimeBegin);
&&&&&&&&&&&&&&&$monthEnd=(date("Y",$dateTimeEnd)*12)+
&&&&&&&&&&&&&&&&&date("n",$dateTimeEnd);
&&&&&&&&&&&&&&&$monthDiff=$monthEnd-$monthBegin;
&&&&&&&&&&&&&&&return($monthDiff);
&&&&&&&&&&&case&"yyyy":&//similar&result&"yyyy"&dateDiff&Microsoft
&&&&&&&&&&&&&&&return(date("Y",$dateTimeEnd)&-&date("Y",$dateTimeBegin));
&&&&&&&&&&&default:
&&&&&&&&&&&&&&&return(floor($dif/86400));&//86400s=1d
&&&&&&&&&}
&&&&&&&}
?&
Calculate&Sum&BusinessDays&(Mon&till&Fri)&between&two&date's&:&?phpfunction&businessdays($begin,&$end)&{&&&&$rbegin&=&is_string($begin)&?&strtotime(strval($begin))&:&$begin;&&&&$rend&=&is_string($end)&?&strtotime(strval($end))&:&$end;&&&&if&($rbegin&&&0&||&$rend&&&0)&&&&&&&&return&0;&&&&$begin&=&workday($rbegin,&TRUE);&&&&$end&=&workday($rend,&FALSE);&&&&if&($end&&&$begin)&{&&&&&&&&$end&=&$begin;&&&&&&&&$begin&=&$end;&&&&}&&&&$difftime&=&$end&-&$begin;&&&&$diffdays&=&floor($difftime&/&(24&*&60&*&60))&+&1;&&&&if&($diffdays&&&7)&{&&&&&&&&$abegin&=&getdate($rbegin);&&&&&&&&$aend&=&getdate($rend);&&&&&&&&if&($diffdays&==&1&&&&($astart['wday']&==&0&||&$astart['wday']&==&6)&&&&($aend['wday']&==&0&||&$aend['wday']&==&6))&&&&&&&&&&&&return&0;&&&&&&&&$abegin&=&getdate($begin);&&&&&&&&$aend&=&getdate($end);&&&&&&&&$weekends&=&($aend['wday']&&&$abegin['wday'])&?&1&:&0;&&&&}&else&&&&&&&&$weekends&=&floor($diffdays&/&7);&&&&return&$diffdays&-&($weekends&*&2);}function&workday($date,&$begindate&=&TRUE)&{&&&&$adate&=&getdate($date);&&&&$day&=&24&*&60&*&60;&&&&if&($adate['wday']&==&0)&//&Sunday&&&&&&&&$date&+=&$begindate&?&$day&:&-($day&*&2);&&&&elseif&($adate['wday']&==&6)&//&Saterday&&&&&&&&$date&+=&$begindate&?&$day&*&2&:&-$day;&&&&return&$date;}?&
This&dateDiff()&function&can&take&in&just&about&any&timestamp,&including&UNIX&timestamps&and&anything&that&is&accepted&by&strtotime().&It&returns&an&array&with&the&ability&to&split&the&result&a&couple&different&ways.&I&built&this&function&to&suffice&any&datediff&needs&I&had.&Hope&it&helps&others&too.&?php&&/*********&dateDiff()&function&**********&&&*&returns&Array&of&Int&values&for&difference&between&two&dates&&&*&$date1&&&$date2&--&&positive&integers&are&returned&&&*&$date1&&&$date2&--&&negative&integers&are&returned&&&*&&&*&$split&recognizes&the&following:&&&*&&&'yw'&=&splits&up&years,&weeks&and&days&(default)&&&*&&&'y'&&=&splits&up&years&and&days&&&*&&&'w'&&=&splits&up&weeks&and&days&&&*&&&'d'&&=&total&days&&&*&&&*&examples:&&&*&&&$dif1&=&dateDiff()&or&dateDiff('yw')&&&*&&&$dif2&=&dateDiff('y')&&&*&&&$dif3&=&dateDiff('w')&&&*&&&$dif4&=&dateDiff('d')&&&*&&&*&assuming&dateDiff&returned&853&days,&the&above&&&*&examples&would&have&a&print_r&output&of:&&&*&&&$dif1&==&Array(&[y]&=&&2&[w]&=&&17&[d]&=&&4&)&&&*&&&$dif2&==&Array(&[y]&=&&2&[d]&=&&123&)&&&*&&&$dif3&==&Array(&[w]&=&&121&[d]&=&&6&)&&&*&&&$dif4&==&Array(&[d]&=&&847&)&&&*&&&*&note:&[h]&(hours),&[m]&(minutes),&[s]&(seconds)&are&always&returned&as&elements&of&the&Array&&&*/&&function&dateDiff($dt1,&$dt2,&$split='yw')&{&&&&$date1&=&(strtotime($dt1)&!=&-1)&?&strtotime($dt1)&:&$dt1;&&&&$date2&=&(strtotime($dt2)&!=&-1)&?&strtotime($dt2)&:&$dt2;&&&&$dtDiff&=&$date1&-&$date2;&&&&$totalDays&=&intval($dtDiff/(24*60*60));&&&&$totalSecs&=&$dtDiff-($totalDays*24*60*60);&&&&$dif['h']&=&$h&=&intval($totalSecs/(60*60));&&&&$dif['m']&=&$m&=&intval(($totalSecs-($h*60*60))/60);&&&&$dif['s']&=&$totalSecs-($h*60*60)-($m*60);&&&&//&set&up&array&as&necessary&&&&switch($split)&{&&&&case&'yw':&#&split&years-weeks-days&&&&&&$dif['y']&=&$y&=&intval($totalDays/365);&&&&&&$dif['w']&=&$w&=&intval(($totalDays-($y*365))/7);&&&&&&$dif['d']&=&$totalDays-($y*365)-($w*7);&&&&&&&&&&case&'y':&#&split&years-days&&&&&&$dif['y']&=&$y&=&intval($totalDays/365);&&&&&&$dif['d']&=&$totalDays-($y*365);&&&&&&&&&&case&'w':&#&split&weeks-days&&&&&&$dif['w']&=&$w&=&intval($totalDays/7);&&&&&&$dif['d']&=&$totalDays-($w*7);&&&&&&&&&&case&'d':&#&don't&split&--&total&days&&&&&&$dif['d']&=&$totalDays;&&&&&&&&&&default:&&&&&&die("Error&in&dateDiff().&Unrecognized&\$split&parameter.&Valid&values&are&'yw',&'y',&'w',&'d'.&Default&is&'yw'.");&&&&}&&&&return&$dif;&&}?&
For those who are using pre MYSQL 4.1.1, you can use:
TO_DAYS([Date Value 1])-TO_DAYS([Date Value 2])
For the same result as:
DATEDIFF([Date Value 1],[Date Value 2])
Before you get too advanced using date functions, be sure also to see the calendar functions at http://www.php.net/manual/en/ref.calendar.php .
I&ran&into&an&issue&using&a&function&that&loops&through&an&array&of&dates&where&the&keys&to&the&array&are&the&Unix&timestamp&for&midnight&for&each&date.&&The&loop&starts&at&the&first&timestamp,&then&incremented&by&adding&86400&seconds&(ie.&60&x&60&x&24).&&However,&Daylight&Saving&Time&threw&off&the&accuracy&of&this&loop,&since&certain&days&have&a&duration&other&than&86400&seconds.&&I&worked&around&it&by&adding&a&couple&of&lines&to&force&the&timestamp&to&midnight&at&each&interval.&?php&&$ONE_DAY&=&90000;&&&//&can't&use&86400&because&some&days&have&one&hour&more&or&less&&for&(&$each_timestamp&=&$start_time&;&$each_timestamp&&=&$end_time&;&$each_timestamp&+=&&$ONE_DAY)&{&&&&/*&&force&midnight&to&compensate&for&daylight&saving&time&&*/&&&&$this_timestamp_array&=&getdate(&$each_timestamp&);&&&&$each_timestamp&=&mktime&(&0&,&0&,&0&,&$this_timestamp_array[mon]&,&$this_timestamp_array[mday]&,&$this_timestamp_array[year]&);&&&&&//&do&some&stuff...&&}?&
Additional&thisone&here&(didn't&test&it&yet&but&should&work&:D):&?php/**&*&Calculates&the&Difference&between&two&timestamps&*&*&@param&integer&$start_timestamp&*&@param&integer&$end_timestamp&*&@param&integer&$unit&(default&0)&*&@return&string&*&@access&public&*/function&dateDifference($start_timestamp,$end_timestamp,$unit=&0){&&$days_seconds_star=&(23&*&56&*&60)&+&4.091;&//&Star&Day&&$days_seconds_sun=&24&*&60&*&60;&//&Sun&Day&&$difference_seconds=&$end_timestamp&-&$start_timestamp;&&switch($unit){&&&&case&3:&//&Days&&&&&&$difference_days=&round(($difference_seconds&/&$days_seconds_sun),2);&&&&&&return&'approx.&'.$difference_hours.'&Days';&&&&case&2:&//&Hours&&&&&&$difference_hours=&round(($difference_seconds&/&3600),2);&&&&&&return&'approx.&'.$difference_hours.'&Hours';&&&&&&&&case&1:&//&Minutes&&&&&&$difference_minutes=&round(($difference_seconds&/&60),2);&&&&&&return&'approx.&'.$difference_minutes.'&Minutes';&&&&&&&&default:&//&Seconds&&&&&&if($difference_seconds&&&1){&&&&&&&&return&$difference_seconds.'&Seconds';&&&&&&}&&&&&&else{&&&&&&&&return&$difference_seconds.'&Second';&&&&&&}&&}}?&
A much easier way to do days diff is to use Julian Days from the Calendar functions:
$start = gregoriantojd($smon, $sday, $syear);
$end = gregoriantojd($emon, $eday, $eyear);
$daysdiff = $end - $
You can see the obvious ways to wrap a function around that.
I&needed&to&calculate&the&week&number&from&a&given&date&and&vice&versa,&where&the&week&starts&with&a&Monday&and&the&first&week&of&a&year&may&begin&the&year&before,&if&the&year&begins&in&the&middle&of&the&week&(Tue-Sun).&This&is&the&way&weekly&magazines&calculate&their&issue&numbers.
Here&are&two&functions&that&do&exactly&that:
Hope&somebody&finds&this&useful.
&?php
/*&&w&e&e&k&n&u&m&b&e&r&&--------------------------------------&//
weeknumber&returns&a&week&number&from&a&given&date&(&1970,&&2030)
Wed,&&is&in&week&1
Mon,&&is&in&week&2
Wed,&&is&in&week&53,&next&years&first&week
Be&careful,&there&are&years&with&53&weeks.
//&------------------------------------------------------------&*/&
function&weeknumber&($y,&$m,&$d)&{
&&&&$wn&=&strftime("%W",mktime(0,0,0,$m,$d,$y));
&&&&$wn&+=&0;&#&wn&might&be&a&string&value
&&&&$firstdayofyear&=&getdate(mktime(0,0,0,1,1,$y));
&&&&if&($firstdayofyear["wday"]&!=&1)&&&&#&if&1/1&is&not&a&Monday,&add&1
&&&&&&&&$wn&+=&1;
&&&&return&($wn);
}&&&&#&function&weeknumber
/*&&d&a&t&e&f&r&o&m&w&e&e&k&&----------------------------------&//
From&a&weeknumber,&calculates&the&corresponding&date
Input:&Year,&weeknumber&and&day&offset
Output:&Exact&date&in&an&associative&(named)&array
:&&(a&Monday)
1995,&&53,&2:&1995-12-xx
...
//&------------------------------------------------------------&*/&
function&datefromweek&($y,&$w,&$o)&{
&&&&$days&=&($w&-&1)&*&7&+&$o;
&&&&$firstdayofyear&=&getdate(mktime(0,0,0,1,1,$y));
&&&&if&($firstdayofyear["wday"]&==&0)&$firstdayofyear["wday"]&+=&7;&
#&in&getdate,&Sunday&is&0&instead&of&7
&&&&$firstmonday&=&getdate(mktime(0,0,0,1,1-$firstdayofyear["wday"]+1,$y));
&&&&$calcdate&=&getdate(mktime(0,0,0,$firstmonday["mon"],&$firstmonday["mday"]+$days,$firstmonday["year"]));
&&&&$date["year"]&=&$calcdate["year"];
&&&&$date["month"]&=&$calcdate["mon"];
&&&&$date["day"]&=&$calcdate["mday"];
&&&&return&($date);
}&&&&#&function&datefromweek
?&
I&needed&a&function&that&determined&the&last&Sunday&of&the&month.&Since&it's&made&for&the&website's&"next&meeting"&announcement,&it&goes&based&on&the&system&&also,&if&today&is&between&Sunday&and&the&end&of&the&month,&it&figures&out&the&last&Sunday&of&*next*&month.&lastsunday()&takes&no&arguments&and&returns&the&date&as&a&string&in&the&form&"January&26,&2003".&I&could&probably&have&streamlined&this&quite&a&bit,&but&at&least&it's&transparent&code.&=)
&?php
&&/*&The&two&functions&calculate&when&the&next&meeting&will
&&&&&be,&based&on&the&assumption&that&the&meeting&will&be&on
&&&&&the&last&Sunday&of&the&month.&*/&&
&&function&getlast($mon,&$year)&{
&&&&$daysinmonth&=&array(31,&28,&31,&30,&31,&30,&31,&31,&30,&31,&30,&31);
&&&&$days&=&$daysinmonth[$mon-1];
&&&&if&($mon&==&2&&&&($year&%&4)&==&0&&&&(($year&%&100)&!=&0&||
&&&&($year&%&400)&==&0))&$days++;
&&&&if&($mon&==&2&&&&($year&%&4)&==&0&&&&($year&%&1000)&!=&0)&$days++;
&&&&$lastday&=&getdate(mktime(0,0,0,$mon,$days,$year));
&&&&$wday&=&$lastday['wday'];
&&&&return&getdate(mktime(0,0,0,$mon,$days-$wday,$year));
&&}
&&function&lastsunday()&{
&&&&$today&=&getdate();
&&&&$mon&=&$today['mon'];
&&&&$year&=&$today['year'];
&&&&$mday&=&$today['mday'];
&&&&$lastsun&=&getlast($mon,&$year);
&&&&$sunday&=&$lastsun['mday'];
&&&&if&($sunday&&&$mday)&{
&&&&&&$mon++;
&&&&&&if&($mon&=&13)&{
&&&&&&&&$mon&=&1;
&&&&&&&&$year++;
&&&&&&}
&&&&&&$lastsun&=&getlast($mon,&$year);
&&&&&&$sunday&=&$lastsun['mday'];
&&&&}
&&&&$nextmeeting&=&getdate(mktime(0,0,0,$mon,$sunday,$year));
&&&&$month&=&$nextmeeting['month'];
&&&&$mday&=&$nextmeeting['mday'];
&&&&$year&=&$nextmeeting['year'];
&&&&return&"$month&$mday,&$year";
&&}
?&
I&wanted&to&find&all&records&in&my&database&which&match&the&current&week&(for&a&call-back&function).&I&made&up&this&function&to&find&the&start&and&end&of&the&current&week&:&
&?php
function&week($curtime)&{
&&&&
&&&&$date_array&=&getdate&(time());
&&&&$numdays&=&$date_array["wday"];
&&&&
&&&&$startdate&=&date("Y-m-d",&time()&-&($numdays&*&24*60*60));
&&&&$enddate&=&date("Y-m-d",&time()&+&((7&-&$numdays)&*&24*60*60));
&&&&$week['start']&=&$startdate;
&&&&$week['end']&=&$enddate;
&&&&
&&&&return&$week;
&&&&
}
?&
Someone may find this info of some use:
Rules for calculating a leap year:
1) If the year divides by 4, it is a leap year (, 1996 are leap years)
2) Unless it divides by 100, in which case it isn't (1900 divides by 4, but was not a leap year)
3) Unless it divides by 400, in which case it is actually a leap year afterall (So 2000 was a leap year).
In practical terms, to work out the number of days in X years, multiply X by 365.2425, rounding DOWN to the last whole number, should give you the number of days.
The result will never be more than one whole day inaccurate, as opposed to multiplying by 365, which, over more years, will create a larger and larger deficit.
I had some problems with dates between mySQL and PHP.
PHP had all these great date functions but I wanted to store a usable value in my database tables. In this case I was using TIMESTAMP(14)
&or 'YYYYMMDDHHMMSS'&.
This is perhaps the easiest way I have found to pull the PHP usable UNIX Datestamp from my mySQL datestamp stored in the tables:
Use the mySQL UNIX_TIMESTAMP() function in your SQL definition string. i.e.
$sql= &SELECT field1, field2, UNIX_TIMESTAMP(field3) as your_date
FROM your_table
WHERE field1 = '$value'&;
The query will return a temp table with coulms &field1& &Field2& &your_date&
The &your_date& will be formatted in a UNIX TIMESTAMP!
Now you can use the PHP date() function to spew out nice date formats.
Sample using above $sql:
= Date Stored on mySQL table (TIMESTAMP(14))
= value returned as your_date in sql stmt (UNIX_TIMESTAMP)
if we use $newdate = date(&F jS, Y -- g:ia&, $row[&your_date&]);
--(after fetching our array from the sql results of course)--
echo &$newdate&;
--Will produce:
January 11th, 2001 -- 12:27am
Hope this helps someone out there!

我要回帖

更多关于 java int转化为date 的文章

 

随机推荐