php函数入门学习(date&time&strtotime)

1.date()

date()函数是 PHP 中用于格式化日期和时间的一个非常常用的函数。它可以根据指定的格式字符串返回当前时间或指定时间的日期和时间。

 

基本语法:

string date ( string $format [, int $timestamp = time() ] )

- `$format`:一个格式化字符串,定义了输出的日期和时间的格式。
- `$timestamp`:一个可选的 Unix 时间戳。如果没有提供,则默认使用当前时间。

 

常用格式化字符:

- `d`:月份中的第几天(两位数字,带前导零),例如:`01` 到 `31`
- `m`:月份(两位数字,带前导零),例如:`01` 到 `12`
- `Y`:四位数的年份,例如:`2023`
- `y`:两位数的年份,例如:`23`
- `H`:小时(24 小时制,两位数字,带前导零),例如:`00` 到 `23`
- `i`:分钟(两位数字,带前导零),例如:`00` 到 `59`
- `s`:秒(两位数字,带前导零),例如:`00` 到 `59`
- `A`:大写的 AM 或 PM
- `a`:小写的 am 或 pm

 

示例

1. 获取当前日期和时间:
<?php

echo date('Y-m-d H:i:s');
// 输出:2024-07-09 13:32:09

 

2. 获取当前年份:
<?php

echo date('Y');

// 输出:2024

3. 获取当前月份的第几天:
<?php

echo date('d');

// 输出类似:09

 

4. 使用 Unix 时间戳:
<?php
$timestamp = 1609459200; // 2021-01-01 00:00:00
echo date('Y-m-d H:i:s', $timestamp);

// 输出:2021-01-01 00:00:00

注意事项

- `date()` 函数返回的时间是基于服务器的默认时区。如需设置特定的时区,可使用 `date_default_timezone_set()` 函数。如:

<?php
date_default_timezone_set('America/New_York');
echo date('Y-m-d H:i:s'); // 输出纽约时区的当前时间
 
------
 

2.time()

用于获取当前的 Unix 时间戳。Unix 时间戳是从 Unix 纪元(1970 年 1 月 1 日 00:00:00 GMT)开始到当前时间的秒数。

返回值

time()函数返回一个整数,表示当前的 Unix 时间戳。

示例:

<?php
// 获取当前的 Unix 时间戳
$currentTimestamp = time();
echo "当前的 Unix 时间戳是: " . $currentTimestamp . "\n";

// 将时间戳格式化为可读的日期和时间
$readableDate = date("Y-m-d H:i:s", $currentTimestamp);
echo "当前的日期和时间是: " . $readableDate . "\n";
?>
// 输出:当前的 Unix 时间戳是: 1720503569 当前的日期和时间是: 2024-07-09 13:39:29

 

适用场景:

1. 记录当前时间
2. 计算时间差
3. 生成唯一标识符

 

注意事项

- time()函数返回的时间戳是基于服务器的时区设置的。如果需要处理不同时区的时间,可使用 `date_default_timezone_set()` 函数来设置时区。
- Unix 时间戳在 2038 年 1 月 19 日 03:14:07(UTC)之后会溢出(对于 32 位系统),这被称为 2038 年问题。现代的 64 位系统不受此限制。

示例:

<?php
// 设置时区为东八区(中国标准时间)
date_default_timezone_set('Asia/Shanghai');

// 获取当前的 Unix 时间戳
$currentTimestamp = time();
echo "当前的 Unix 时间戳是: " . $currentTimestamp . "\n";

// 将时间戳格式化为可读的日期和时间
$readableDate = date("Y-m-d H:i:s", $currentTimestamp);
echo "当前的日期和时间是: " . $readableDate . "\n";
?>

// 输出:当前的 Unix 时间戳是: 1720503749 当前的日期和时间是: 2024-07-09 13:42:29

 

3.strtotime()

用于将英文文本格式的日期时间描述解析为 Unix 时间戳(自1970年1月1日00:00:00 UTC以来的秒数),可以解析多种格式的日期和时间字符串。

 

语法

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

参数

- $time: 一个包含英文日期时间描述的字符串。
- $now: 可选参数,表示用于计算相对日期的时间戳。如果未提供,默认使用当前时间。

 

返回值

成功时返回解析后的 Unix 时间戳,失败时返回 `false`。

 

示例

1. 基本用法

<?php
$timestamp = strtotime("now");
echo $timestamp; // 输出当前时间的时间戳
?>

// 输出:1720503994

 

2. 解析具体日期

<?php
$timestamp = strtotime("2023-10-01");
echo date("Y-m-d H:i:s", $timestamp);
?>
// 输出:2023-10-01 00:00:00

 

3. 相对时间

<?php
$timestamp = strtotime("+1 week");
echo date("Y-m-d H:i:s", $timestamp); // 输出当前时间加一周后的日期时间
?>
// 输出:2024-07-16 13:48:14

 

4. 复杂的相对时间

<?php
$timestamp = strtotime("last Monday");
echo date("Y-m-d H:i:s", $timestamp); // 输出上一个星期一的日期时间
?>
// 输出:2024-07-08 00:00:00

 

5. 结合当前时间

<?php
$now = time();
$timestamp = strtotime("+2 days", $now);
echo date("Y-m-d H:i:s", $timestamp); // 输出当前时间加两天后的日期时间
?>
// 输出:2024-07-11 13:50:08

 

注意事项

- strtotime()函数对输入的字符串格式有一定的要求,某些不标准的格式可能无法正确解析。
- 如果解析失败,函数会返回 `false`,因此在使用时最好进行错误检查。

错误检查示例

<?php
$timestamp = strtotime("+2 daysss");

if ($timestamp === false) {
echo "解析日期失败";
} else {
echo date("Y-m-d H:i:s", $timestamp);
}
?>
// 输出:解析日期失败

 

----------------------------------

滴滴滴随堂练习

----------------------------------

<?php

// 1、写一个函数,传入日期(形如 2024-06-22),返回该日期所在月份的月初、月末日期

function getFirstAndLastDate($str){
    if(!$str) return ['',''];
    // 分别获取月份第一天与最后一天的日期时间戳,转成日期格式返回
    $date = date("Ymd", strtotime($str));
    $firstDayTimestamp = strtotime("first day of $date");
    $lastDayTimestamp = strtotime("last day of $date");
    return [date("Y-m-d", $firstDayTimestamp), date("Y-m-d", $lastDayTimestamp)];
}

print_r(getFirstAndLastDate('2024-08-03'));
echo "<br>";
 
老师反馈:
还可以这样 $month_start = date("Y-m-01", strtotime($a)); $month_last = date("Y-m-d", strtotime("+1month-1day", strtotime($month_start)));
 

// 2、写一个函数,传入日期(形如 2024-06-22),判断该日期出生的牛马是否已成年

function getIsEighteen($str){
    if(!$str) return '请输入出生日期';
    $currentTimestamp  = time();
    $birthTimestamp = strtotime($str);
    // 只判断年份够不够18
    // $age = date("Y") - date("Y", $birthTimestamp);
    // 爷直接给你按一年365天算
    $yearsDiff = abs($currentTimestamp - $birthTimestamp) / (24*60*60*365);
    if( $yearsDiff >= 18 ){
        return '该牛马已成年';
    }else{
        return '该牛马未成年';
    }
}

echo getIsEighteen("2006-07-22");
echo "<br>";

// 3、写一个函数,传入两个日期(形如 2024-06-22),返回两个时间之间相差的小时数(如不超1天),天数(超1天)

function getDiffTime($str1,$str2){
    if(!$str1 || !$str2) return '请传入两个日期';
    $daysDiff = abs(strtotime($str1) - strtotime($str2)) / (24*60*60);
    // 若相差天数大于等于1,返回相差天数(向上取整

    if($daysDiff >= 1){
        $daysDiffEcho = ceil($daysDiff);
        return "相差".$daysDiffEcho."天";
    }else{
        $hoursDiff = abs(strtotime($str1) - strtotime($str2)) / (60*60);
        // 否则,返回相差小时数(向上取整
        $isTimeSame = $hoursDiff === 0 ? true : false;
        $hoursDiffEcho = ceil($hoursDiff);
        return $isTimeSame ? "日期时间相同" : "相差".$hoursDiffEcho."小时";
    }
}
echo getDiffTime("2014-06-22 12:10:00", "2014-06-24 12:20:00");
echo "<br>";
?>
 
posted @ 2024-07-09 13:53  芝麻小仙女  阅读(1)  评论(0编辑  收藏  举报