一、UNIX时间戳
保存日期和时间的方法
二、日期和时间转变为UNIX时间戳
1.
mktime(hour,minute,second,month,day,year)、
代码:
运行:
2、
strtotime(time/now)
代码:
运行:
3、案例:
代码:
/倒计时
$now=@strtotime("now");//获取系统当前时间
$endtime=@strtotime("2024-06-30 08:08:08");//设置截止时间,转为时间戳
$second=$endtime-$now;//当前到截止时间的时间戳
$year=floor($second/3600/24/365);//floor向下取整
$temp=$second-$year*365*24*3600;
$month=floor($temp/3600/24/30);
$temp=$temp-@ $mouth*30*24*3600;
$day=floor($temp/3600/24);
$temp=$temp-$day*24*3600;
$hour=floor($temp/3600);
$temp=$temp-$hour*3600;
$minute=floor($temp/60);
$second=$temp-$minute*60;
echo "距离截止时间还有:{$year}年{$month}月{$day}日{$hour}小时{$second}分{$minute}秒";
运行:
三、日期的计算
代码:
//通过出生日期计算年龄
$year=2001;
$month=10;
$day=24;
$birthday=@mktime(0,0,0,$month,$day,$year);
$nowdate=time();
$ageunix=$nowdate-$birthday;
$age=floor($ageunix/(60*60*24*365));
echo "该用户的年龄为{$age}岁";
运行:
四、获取日期和时间
1.获取当前时间:time()
2.getdate(时间戳的形式)
$array1=getdate();
$array2=getdate(strtotime("now"));
print_r($array1);
echo "<br>";
print_r($array2);
4、date()格式化本地时间和日期
echo date('jS-F-Y')."<br>";
echo date('Y-m-d')."<br>";
echo date('1M',strtotime('2019-09-10'))."<br>";
echo date("1",mktime(0,0,0,2,14,2019));
echo date('U');