大家好
其實我當初開這部落格
是希望多分享一些程式相關文章
但是不知道為什麼
貼了很多雜七雜八文章
主要也應該是覺得有趣
或者是幫忙保留一些快要被刪除的文章吧
以上是題外話
 
在PHP中
我們取得時間的方式有很多種
但是要取得某個時候的時間
就需要比較特別的描述
 
先舉
的例子
 
<?php
//都是取得UNIX時間
echo(strtotime("now")).'<br>';//現在
echo(strtotime("3 October 2005")).'<br>';//2005年10月3日
echo(strtotime("+5 hours")).'<br>';//現在5小時後
echo(strtotime("+1 week")).'<br>';//現在一個禮拜後
echo(strtotime("+1 week 3 days 7 hours 5 seconds")).'<br>';//現在一個禮拜3天7小時5秒後
echo(strtotime("next Monday")).'<br>';//下一個星期一
echo(strtotime("last Sunday")).'<br>';//上一個星期日
echo(strtotime("next Month")).'<br>';//下一個月
?>
 
但是會發現輸出都是UNIX時間
所以配合date() 函式
 
<?php
echo date('現在 是 Y年m月d日 H分i時s秒',strtotime("now")).'<br>';//現在
echo date('2005年10月3日 是 Y年m月d日 H分i時s秒',strtotime("3 October 2005")).'<br>';//2005年10月3日
echo date('現在5小時後 是 Y年m月d日 H分i時s秒',strtotime("+5 hours")).'<br>';//現在5小時後
echo date('現在一個禮拜後 是 Y年m月d日 H分i時s秒',strtotime("+1 week")).'<br>';//現在一個禮拜後
echo date('現在一個禮拜3天7小時5秒後 是 Y年m月d日 H分i時s秒',strtotime("+1 week 3 days 7 hours 5 seconds")).'<br>';//現在一個禮拜3天7小時5秒後
echo date('下一個星期一 是 Y年m月d日 H分i時s秒',strtotime("next Monday")).'<br>';//下一個星期一
echo date('上一個星期日 是 Y年m月d日 H分i時s秒',strtotime("last Sunday")).'<br>';//上一個星期日
echo date('下一個月 是 Y年m月d日 H分i時s秒',strtotime("next Month")).'<br>';//下一個月
?>

所以很簡單吧
用說的
就可以取得要的時間
 
官網有更詳細的說明
 
 
Used Symbols
DescriptionFormat
dayname 'sunday' | 'monday' | 'tuesday' | 'wednesday' | 'thursday' | 'friday' | 'saturday' | 'sun' | 'mon' | 'tue' | 'wed' | 'thu' | 'fri' | 'sat'
daytext 'weekday' | 'weekdays'
number [+-]?[0-9]+
ordinal 'first' | 'second' | 'third' | 'fourth' | 'fifth' | 'sixth' | 'seventh' | 'eighth' | 'ninth' | 'tenth' | 'eleventh' | 'twelfth' | 'next' | 'last' | 'previous' | 'this'
reltext 'next' | 'last' | 'previous' | 'this'
space [ \t]+
unit (('sec' | 'second' | 'min' | 'minute' | 'hour' | 'day' | 'fortnight' | 'forthnight' | 'month' | 'year') 's'?) | 'weeks' |daytext
Day-based Notations
FormatDescriptionExamples
'yesterday' Midnight of yesterday "yesterday 14:00"
'midnight' The time is set to 00:00:00  
'today' The time is set to 00:00:00  
'now' Now - this is simply ignored  
'noon' The time is set to 12:00:00 "yesterday noon"
'tomorrow' Midnight of tomorrow  
'back of'hour 15 minutes past the specified hour "back of 7pm", "back of 15"
'front of'hour 15 minutes before the specified hour "front of 5am", "front of 23"
'first day of' Sets the day of the first of the current month. This phrase is best used together with a month name following it. "first day of January 2008"
'last day of' Sets the day to the last day of the current month. This phrase is best used together with a month name following it. "last day of next month"
ordinalspacedaynamespace'of' Calculates thex-th week day of the current month. "first sat of July 2008"
'last'spacedaynamespace'of' Calculates thelastweek day of the current month. "last sat of July 2008"
numberspace? (unit| 'week') Handles relative time items where the value is a number. "+5 weeks", "12 day", "-7 weekdays"
ordinalspaceunit Handles relative time items where the value is text. "fifth day", "second month"
'ago' Negates all the values of previously found relative time items. "2 days ago", "8 days ago 14:00", "2 months 5 days ago", "2 months ago 5 days", "2 days ago"
dayname Moves to the next day of this name. "Monday"
reltextspace'week' Handles the special format "weekday + last/this/next week". "Monday next week"
 
還有問題請問我喔
感恩!