標籤:echo function ons set date selected 列印 mysq test
答案1:
<?phpecho date(‘Y‘.‘年‘.‘m‘.‘月‘.‘d‘.‘日‘.‘ H:i:s‘,strtotime(‘-1 day‘));
輸出結果:
Warning: strtotime(): It is not safe to rely on the system‘s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone ‘UTC‘ for now, but please set date.timezone to select your timezone. in C:\AppServ\www\test2.php on line 2Warning: date(): It is not safe to rely on the system‘s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected the timezone ‘UTC‘ for now, but please set date.timezone to select your timezone. in C:\AppServ\www\test2.php on line 22017-03-01 15:06:41
答案2:
<?phpdate_default_timezone_set(‘UTC‘);echo date(‘Y‘.‘年‘.‘m‘.‘月‘.‘d‘.‘日‘.‘ H:i:s‘,strtotime(‘-1 day‘));
運行結果:
2017年03月01日 15:06:43
date() — 格式化一個本地時間/日期
string date ( string $format [, int $timestamp ] )
返回將整數 timestamp 按照給定的格式字串而產生的字串。如果沒有給出時間戳記則使用本地目前時間。換句話說,timestamp 是可選的,預設值為 time()。
Note:要將字串表達的時間轉換成時間戳記,應該使用 strtotime()。此外一些資料庫有一些函數將其時間格式轉換成時間戳記(例如 MySQL 的 ? UNIX_TIMESTAMP 函數)。
strtotime() — 將任何字串的日期時間描述解析為 Unix 時間戳記
定義和用法
strtotime() 函數將任何英文文本的日期或時間描述解析為 Unix 時間戳記(自 January 1 1970 00:00:00 GMT 起的秒數)。
int strtotime ( string $time [, int $now = time() ] )
參數
-
time:日期/時間字串。正確格式的說明詳見 日期與時間格式。
-
now:用來計算傳回值的時間戳記。如果省略該參數,則使用目前時間。
傳回值
成功則返回時間戳記,否則返回 FALSE。在 PHP 5.1.0 之前本函數在失敗時返回 -1。
執行個體
將英文文本日期時間解析為 Unix 時間戳記:
<?phpecho(strtotime("now") . "<br>");echo(strtotime("15 October 1980") . "<br>");echo(strtotime("+5 hours") . "<br>");echo(strtotime("+1 week") . "<br>");echo(strtotime("+1 week 3 days 7 hours 5 seconds") . "<br>");echo(strtotime("next Monday") . "<br>");echo(strtotime("last Sunday"));?>
運行結果:
1488465569147300480014884835691489070369148935477414887296001488038400
http://www.w3school.com.cn/php/func_date_strtotime.asp
http://php.net/manual/zh/function.strtotime.php
mktime — 取得一個日期的 Unix 時間戳記
日期轉換為時間戳記
PHP 提供了函數可以方便的將各種形式的日期轉換為時間戳記,該類函數主要是:
- strtotime():將任何英文文本的日期時間描述解析為時間戳記。
- mktime():從日期取得時間戳記。
用PHP列印出前一天的時間,列印格式是2007年5月10日22:21:21