Maison  >  Article  >  développement back-end  >  Comment obtenir l'horodatage quelques jours plus tard en php

Comment obtenir l'horodatage quelques jours plus tard en php

青灯夜游
青灯夜游original
2021-12-08 11:07:014065parcourir

在php中,可以使用strtotime()函数获取几天后的时间戳,该函数可以用来计算一些日期时间的间隔,并返回间隔几天的时间戳;语法“strtotime('+数值 day')”,“数值”用于规定间隔的天数。

Comment obtenir l'horodatage quelques jours plus tard en php

本教程操作环境:windows7系统、PHP7.1版、DELL G3电脑

在php中,可以使用strtotime()函数获取几天后的时间戳。

strtotime() 函数有两种用法:一种是将字符串形式的、用英文文本描述的日期时间解析为 UNIX 时间戳,一种是用来计算一些日期时间的间隔。

php 获取几天后的时间戳可以使用以下语句:

<?php
header("Content-type:text/html;charset=utf-8");
$d=strtotime(&#39;+10 day&#39;);//获取10天后的时间戳
echo $d."<br><br>"; 
$t=date("Y-m-d h:i:s",$d);
echo "获取的时间戳代表: ".$t;
?>

Comment obtenir lhorodatage quelques jours plus tard en php

Comment obtenir lhorodatage quelques jours plus tard en php

使用strtotime() 函数进行日期的加减运算

有时我们需要在一个日期上加减一定的时间间隔。可以使用 strtotime() 来计算一些日期时间间隔。示例如下:

<?php
header("Content-type:text/html;charset=utf-8");
$start = &#39;last Monday&#39;;
$interval = strtotime("$start + 4 days");
echo "现在\$interval 表示上周的" . date(&#39;l&#39;,$interval);
?>

执行以上程序的输出结果为:

Comment obtenir lhorodatage quelques jours plus tard en php

如果日期使用时间戳表示,并且时间间隔也可用秒来表示,就可以从时间戳中减去时间间隔。示例如下;

<?php
header("Content-type:text/html;charset=utf-8");
$start = time();
echo date(&#39;Y-m-d&#39;,$start) , "<br />";
$interval = 7 * 24 * 3600;  //一周
$end = $start - $interval;
echo date(&#39;Y-m-d&#39;,$end);
?>

执行以上程序的输出结果为:

Comment obtenir lhorodatage quelques jours plus tard en php

前后两个日期正好相差 7 天。

推荐学习:《PHP视频教程

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Déclaration:
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn