日期的轉換只要把日期轉成 Timestamp, 就可以使用 date() 轉成自己想要的格式.
strtotime() 很常用, 通常都是把 "2012-02-12 22:11:10" 丟進去, 轉成 Timestamp.
最近想要抓某個日期, 或者是上週、下週、上個星期一、下個星期二.. 等等的時間, 才發現 strtotime() 強的很過份. XD
官方文件詳見下述:
PHP strtotime() function 的使用範例
<?php echo strtotime('Monday this week'), "\n"; echo strtotime("10 September 2000"), "\n"; echo strtotime("+1 day"), "\n"; echo strtotime("+1 week"), "\n"; echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n"; echo strtotime("next Thursday"), "\n"; echo strtotime("last Monday"), "\n"; ?>
- $ php -a
php > echo strtotime("10 September 2000"), "\n";
968515200
php > function d($t) {
php { echo date('Y-m-d', $t);
php { }
php > echo d(strtotime("10 September 2000"));
2000-09-10
php > echo d(strtotime("+1 day"));
2012-03-08
php > echo d(strtotime("+1 week"));
2012-03-14
php > echo d(strtotime("-1 week"));
2012-02-29
php > echo d(strtotime("next Thursday"));
2012-03-08
php > echo d(strtotime("Monday this week"));
2012-03-05
php > echo d(strtotime("-5 week"));
2012-02-01
php > echo d(strtotime("Sunday -5 week"));
2012-02-05
php > echo d(strtotime("2012-03-05"));
2012-03-05
php > echo d(strtotime("Sunday 2012-03-07"));
2012-03-11
php > echo d(strtotime("last Sunday 2012-03-07"));
2012-03-04