PHP 日期时间处理

发布于 2021-05-12 23:23 ,所属分类:区块连和PHP开发学习资料

date_default_timezone_set(‘Asia/Shanghai’); //设置时区


PHP时间参数:

  • a - "am" 或是 "pm"

  • A - "AM" 或是 "PM"

  • d - 几日,二位数字,若不足二位则前面补零; 如: "01" 至 "31"

  • D - 星期几,三个英文字母; 如: "Fri"

  • F - 月份,英文全名; 如: "January"

  • h - 12 小时制的小时; 如: "01" 至 "12"

  • H - 24 小时制的小时; 如: "00" 至 "23"

  • g - 12 小时制的小时,不足二位不补零; 如: "1" 至 12"

  • G - 24 小时制的小时,不足二位不补零; 如: "0" 至 "23"

  • i - 分钟; 如: "00" 至 "59"

  • j - 几日,二位数字,若不足二位不补零; 如: "1" 至 "31"

  • l - 星期几,英文全名; 如: "Friday"

  • m - 月份,二位数字,若不足二位则在前面补零; 如: "01" 至 "12"

  • n - 月份,二位数字,若不足二位则不补零; 如: "1" 至 "12"

  • M - 月份,三个英文字母; 如: "Jan"

  • s - 秒; 如: "00" 至 "59"

  • S - 字尾加英文序数,二个英文字母; 如: "th","nd"

  • t - 指定月份的天数; 如: "28" 至 "31"

  • U - 总秒数

  • w - 数字型的星期几,如: "0" (星期日) 至 "6" (星期六)

  • Y - 年,四位数字; 如: "1999"

  • y - 年,二位数字; 如: "99"

  • z - 一年中的第几天; 如: "0" 至 "365"

  • time - 规定一个日期/时间字符串。NULL 表示当前的日期/时间。

  • timezone - 规定 time 的时区。默认为当前时区。



PHP中对日期进行处理常用的几个函数如下:


  • date(): 把时间戳格式化为更易读的日期和时间

  • time(): 获取当前 Unix 时间戳

  • strtotime(): 将表示时间和日期的字符串转化为相应的时间戳

  • mktime(): 创建日期


这些函数是PHP核心的部分,无需安装即可使用。另外需要注意的是,这些函数的行为还受到 php.ini 中配置的时区等的影响。



date() 函数


格式:date(‘时间格式’[,’时间戳’]);

<?php// 假定今天是:March 10th, 2001, 5:16:18 pm$today = date("Y-m-d H:i:s");                   // 2001-03-10 17:16:18 (MySQL DATETIME 格式)$today = date("F j, Y, g:i a");                 // March 10, 2001, 5:16 pm$today = date("m.d.y");                         // 03.10.01$today = date("j, n, Y");                       // 10, 3, 2001$today = date("Ymd");                           // 20010310$today = date('h-i-s, j-m-y, it is w Day z ');  // 05-16-17, 10-03-01, 1631 1618 6 Fripm01$today = date('\i\t \i\s \t\h\e jS \d\a\y.');   // It is the 10th day.$today = date("D M j G:i:s T Y");               // Sat Mar 10 15:16:08 MST 2001$today = date('H:m:s \m \i\s\ \m\o\n\t\h');     // 17:03:17 m is month$today = date("H:i:s");                         // 17:16:17
date('Y'); // 当前年份date('m'); // 当前月份date('d'); // 当前是几号


strtotime() 函数


语法:int strtotime ( string $time [, int $now = time() ] )


<?phpecho strtotime("now"), "\n";  // 现在时间戳echo strtotime("10 September 2000"), "\n"; // 2000年10月现在时间戳echo strtotime("+1 day"), "\n";  // 距离现在一天后的时间戳echo strtotime("-3 day"), "\n";  // 距离现在三天前的时间戳echo strtotime("+1 week"), "\n"; // 距离现在一周后的时间戳echo strtotime("-1 month"), "\n";// 距离现在一个月前的时间戳echo strtotime("+1 year"), "\n"; // 距离现在一年后的时间戳echo strtotime("+1 week 2 days 4 hours 2 seconds"), "\n";  // 距离现在1周2天4小时2秒后的时间戳echo strtotime("next Thursday"), "\n";  // 下个星期三echo strtotime("last Monday"), "\n";    // 本月的最后一个星期一


mktime() 函数


语法:mktime(hour,minute,second,month,day,year,is_dst);


<?php$lastday = mktime(0, 0, 0, 3, 0, 2000);echo strftime("Last day in Feb 2000 is: %d", $lastday);$lastday = mktime(0, 0, 0, 4, -31, 2000);echo strftime("Last day in Feb 2000 is: %d", $lastday);?> 



时间戳和时间字符串:


使用 time() 函数,会获取当前时间的 Unix 时间戳,是一个10位的整数,表示自 Unix 纪元(1月1日 1970 00:00:00 GMT)起的当前时间的秒数。

使用 strtotime() 函数,可以将任何英文文本的日期或时间描述解析为 Unix 时间戳。失败则返回 FALSE。应该尽可能使用 YYYY-MM-DD 格式或者使用 date_create_from_format() 函数

使用 date() 函数,可以将时间戳按照指定的格式格式化为时间字符串

$time = time(); // 当前时间戳var_dump($time);  // int(1516155874)
$time_str = date('Y-m-d H:i:s', $time); // 将时间戳转化为相应的时间字符串var_dump($time_str); // string(19) "2018-01-17 02:24:34"
$time_int = strtotime($time_str); // 将时间字符串转化为时间戳var_dump($time_int); // int(1516155874)


常用时间获取:


获取那种基于某个时间一定时间段的时间的做法,可以使用 strtotime(),也可以 time() 获取当前时间然后加上或减去指定时间距离现在的偏移秒数。


  • 获取前一天的时间戳:strtotime('-1 day') 或者 time() - 3600 * 24

  • 获取今天凌晨0点的时间戳:strtotime(date("Y-m-d"), time())

  • 获取今天某个时刻如6点半的时间戳:strtotime(data('Y-m-d')) + 6 * 3600 + 30 * 60


<?phpecho "今天:".date("Y-m-d")."<br>";    echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>";    echo "明天:".date("Y-m-d",strtotime("+1 day")). "<br>"; echo "一周后:".date("Y-m-d",strtotime("+1 week")). "<br>";    echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>";    echo "下个星期四:".date("Y-m-d",strtotime("next Thursday")). "<br>";    echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>";    echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>";    echo"一个月后:".date("Y-m-d",strtotime("+1month"))."<br>";echo"十年后:".date("Y-m-d",strtotime("+10year"))."<br>";?>


注:

在数据库中保存为 timastamp 或者 datetime 类型的数据,在PHP中查询时,需要使用时间字符串进行查询,而且查询结果也是时间字符串。

另外如果是使用 int 类型保存的时间戳,则要使用时间戳进行查询。查询结果是时间戳。


相关资源