Summary of issues related to PHP time calculation_php skills

WBOY
Release: 2016-05-16 09:00:03
Original
2083 people have browsed it

the examples in this article summarize issues related to php time calculation. share it with everyone for your reference, the details are as follows:

1. get the date three months ago with php

<?php header("content-type: text/html; charset=utf-8");?>
<?php
$s_sdate=date("y-m-d"); //当前时间
$moth_day=90; //月份 (转为天数)
$s_edate=date("y-m-d",(strtotime($s_sdate)-$moth_day*84600));
echo $moth_day."前的日期为".$s_edate;
?>

Copy after login

2. 30 days after the calculation date

you can use strtotime. php provides a super simple way to complete work that would otherwise require dozens of lines of code.

first convert a date into a unix timestamp

$t = time(); // 当前时间戳
$t = strtotime("+30 days", $t); // 30天后的时间戳
echo date("y-m-d", $t); // 格式化日期

Copy after login

3. convert the timestamps of 2 dates...and then subtract them

$t1 = strtotime("2009-08-19");
$t2 = strtotime("2009-08-20");
$t = $t2 - $t1; // 相差天数的秒
echo (int)($t / 86400)

Copy after login

4. determine whether it is this week

$date = "2008-12-08";
if (isCurrentWeeks($date)) {
  echo $date."是本星期";
} else {
  echo $date."不是本星期";
}
function isCurrentWeeks($d) {
  return (date("W",strtotime($d))==date("W",strtotime("now")));
}

Copy after login

readers who are interested in more php-related content can check out the special topics on this site: "php date and time usage summary", "php string (string) usage summary", "complete of php array (array) operation skills", "summary of php network programming skills", "php basic syntax introductory tutorial", "php skills for operating office documents summary (including word, excel, access, ppt)", "php object-oriented programming introductory tutorial", " php mysql database operation introductory tutorial" and "php summary of common database operation skills

i hope this article will be helpful to everyone in php programming.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template