顯示具有 PHP 標籤的文章。 顯示所有文章
顯示具有 PHP 標籤的文章。 顯示所有文章

2019年5月3日 星期五

PHP 日期加減

PHP 日期加減
1. mktime()
$date1 = date ("Y-m-d H:i:s" , mktime(date('H'), date('i'), date('s'), date('m'), date('d')-1, date('Y'))); 
$date2 = date ("Y-m-d H:i:s" , mktime(date('H'), date('i'), date('s'), date('m'), date('d'), date('Y')));
2. strtotime()
$today = '2013-04-19';
echo date("Y-m-d", strtotime($today."+3 year"));
echo date("Y-m-d", strtotime($today."-1 month"));
echo date("Y-m-d", strtotime($today."+10 week"));
echo date("Y-m-d", strtotime($today."+10 day"));
echo date("Y-m-d", strtotime($today."+2 hour"));
echo date("Y-m-d", strtotime($today."+20 minute"));
echo date("Y-m-d", strtotime($today."+5 seconds"));
echo date("Y-m-d", strtotime($today."+1 day 5 seconds"));
echo date("Y-m-d", strtotime($today."+1 week -2 day"));


參考資料 :
http://blog.twbryce.com/strtotime-method/

PHP Function with Optional Parameters

PHP Function with Optional Parameters

有 2 種方法

1. 用 NULL arguments
function method($arg1 = null, $arg2 = null){
  $arg1 = $arg1? $arg1: "default_value_for_arg1";
  $arg2 = $arg2? $arg2: "default_value_for_arg2";
}
2. 傳 Array
function method($arr){
  $arg1 = $arr['$arg1'] ? $arr['$arg1'] : "default_value_for_arg1";
  $arg2 = $arr['$arg2'] ? $arr['$arg2'] : "default_value_for_arg2";
}

Popular Posts