文章目录 前言一、时间日期与时区1.1 时间与日期1.2 时区 二、 30个日期时间函数的用法示例2.1 获取当前的时间戳2.2 将时间戳格式化为日期时间2.3 获取当前的日期2
PHP中的日期和时间处理功能极其强大,包括了多种格式化、解析、比较等常用操作。以下是常用的函数:
echo date("Y/m/d"); // 输出:2022/02/18
echo time(); // 输出当前时间的时间戳
echo strtotime("2022-02-18 12:00:00"); // 输出:1645209600
echo mktime(0, 0, 0, 2, 18, 2022); // 输出:1645104000
setlocale(LC_TIME, "zh_CN.utf8"); echo strftime("%Y年%m月%d日 %H:%M:%S"); // 输出:2022年02月18日 11:30:00
在 PHP 中设置时区代码可以使用以下代码:
date_default_timezone_set('时区代码');
其中,时区代码可以是任何有效的时区标识符,如 ‘UTC’、‘Asia/Shanghai’ 等。可以使用 PHP 内置函数 timezone_identifiers_list() 查看所有可用的时区列表。例如,以下代码将时区设置为中国上海:
date_default_timezone_set('Asia/Shanghai');
注意:在使用日期和时间相关函数之前,应该先设置时区。否则可能会出现不正确的结果。
$timestamp = time();
echo $timestamp;
$timestamp = time();
$date_time = date('Y-m-d H:i:s', $timestamp);
echo $date_time;
$current_date = date('Y-m-d');
echo $current_date;
$current_time = date('H:i:s');
echo $current_time;
$current_year = date('Y');
echo $current_year;
$current_month = date('m');
echo $current_month;
$current_day = date('j');
echo $current_day;
$start_date = '2021-01-01';
$end_date = '2021-01-31';
$diff_days = (strtotime($end_date) - strtotime($start_date)) / (60 * 60 * 24);
echo $diff_days;
$start_date = '2021-01-01';
$end_date = '2021-03-31';
$diff_months = (intval(date('Y', strtotime($end_date))) - intval(date('Y', strtotime($start_date)))) * 12;
$diff_months -= intval(date('m', strtotime($start_date)));
$diff_months += intval(date('m', strtotime($end_date)));
echo $diff_months;
$start_date = '2021-01-01';
$end_date = '2025-12-31';
$start_year = intval(date('Y', strtotime($start_date)));
$end_year = intval(date('Y', strtotime($end_date)));
$diff_years = $end_year - $start_year;
echo $diff_years;
$current_month_first_day = date('Y-m-01');
echo $current_month_first_day;
$current_month_last_day = date('Y-m-t');
echo $current_month_last_day;
$yesterday = date('Y-m-d', strtotime('-1 day'));
echo $yesterday;
$tomorrow = date('Y-m-d', strtotime('+1 day'));
echo $tomorrow;
$a_week_aGo = date('Y-m-d', strtotime('-1 week'));
echo $a_week_ago;
$a_week_later = date('Y-m-d', strtotime('+1 week'));
echo $a_week_later;
$a_month_ago = date('Y-m-d', strtotime('-1 month'));
echo $a_month_ago;
$a_month_later = date('Y-m-d', strtotime('+1 month'));
echo $a_month_later;
$date = '2020-01-01';
$year = intval(date('Y', strtotime($date)));
$is_leap_year = ($year % 4 == 0 && $year % 100 != 0) || $year % 400 == 0;
echo $is_leap_year ? '是闰年' : '不是闰年';
$next_sunday = date('Y-m-d', strtotime('next Sunday'));
echo $next_sunday;
$next_monday = date('Y-m-d', strtotime('next Monday'));
echo $next_monday;
$current_month_first_sunday = date('Y-m-d', strtotime('first Sunday of ' . date('F Y')));
echo $current_month_first_sunday;
$current_month_first_monday = date('Y-m-d', strtotime('first Monday of ' . date('F Y')));
echo $current_month_first_monday;
$current_month_last_sunday = date('Y-m-d', strtotime('last Sunday of ' . date('F Y')));
echo $current_month_last_sunday;
$current_month_last_monday = date('Y-m-d', strtotime('last Monday of ' . date('F Y')));
echo $current_month_last_monday;
$date = '2021-10-01';
$day_of_week = date('N', strtotime($date));
echo '星期' . intval($day_of_week);
$start_time = '2021-10-01 08:00:00';
$end_time = '2021-10-01 17:00:00';
$diff_time = strtotime($end_time) - strtotime($start_time);
$hours = intval($diff_time / (60 * 60));
$minutes = intval(($diff_time - $hours * 60 * 60) / 60);
$seconds = $diff_time % 60;
echo $hours . '小时' . $minutes . '分钟' . $seconds . '秒';
$last_working_day = date('Y-m-d', strtotime('-1 weekday'));
echo $last_working_day;
$next_working_day = date('Y-m-d', strtotime('+1 weekday'));
echo $next_working_day;
$current_month_working_days = 0;
$current_month_first_day = date('Y-m-01');
$current_month_last_day = date('Y-m-t');
for ($i = strtotime($current_month_first_day); $i <= strtotime($current_month_last_day); $i += 86400) {
$weekday = date('N', $i);
if ($weekday >= 1 && $weekday <= 5) {
$current_month_working_days++;
}
}
echo $current_month_working_days;
$date = '2022-01-01';
$yesterday = date('Y-m-d', strtotime($date) - 24*3600);
echo $yesterday;
以上就是关于本篇文章介绍的内容,时间日期与时区,后续更多内容将收录在专栏PHP快速入门与实战中,感谢大家支持。
来源地址:https://blog.csdn.net/qq_21891743/article/details/130118248
--结束END--
本文标题: PHP快速入门05-时间日期与时区,附30个常用案例
本文链接: https://lsjlt.com/news/556195.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0