


Before explaining the examples, let’s introduce a few core functions:
mktime function
The mktime() function returns the Unix timestamp of a date.
The argument always represents a GMT date, so is_dst has no effect on the result.
The parameters can be left empty in order from right to left, and the empty parameters will be set to the corresponding current GMT value.
Syntax: mktime(hour,minute,second,month,day,year,is_dst)
Parameters
hour Optional. Specified hours.
minute Optional. Specified minutes.
second is optional. Specifies seconds.
month is optional. Specifies the numeric month.
day is optional. Specify days.
year is optional. Specified year. On some systems, legal values are between 1901 - 2038. However, this limitation no longer exists in PHP 5.
is_dst Optional. Set to 1 if the time is during Daylight Saving Time (DST), 0 otherwise, or -1 if unknown.
As of 5.1.0, the is_dst parameter is deprecated. Therefore the new time zone handling features should be used.
Example: The mktime() function is useful for date operations and validation. It can automatically correct out-of-bounds input:
<?php echo(date("M-d-Y",mktime(0,0,0,12,36,2001))); echo(date("M-d-Y",mktime(0,0,0,14,1,2001))); echo(date("M-d-Y",mktime(0,0,0,1,1,2001))); echo(date("M-d-Y",mktime(0,0,0,1,1,99))); ?>
Jan-05-2002 Feb-01-2002 Jan-01-2001 Jan-01-1999
strtotime function
The
strtotime() function parses any English text datetime description into a Unix timestamp.
Syntax:
strtotime(time,now)
Parameter Description
time specifies the time string to be parsed.
now is the timestamp used to calculate the return value. If this parameter is omitted, the current time is used.
One week later: strtotime(" 1 week") ;
One week ago: strtotime("-1 week") ;
After one month: strtotime(" 1 months") ;
One day later: strtotime(" 1 days") ;
After 30 seconds strtotime( " 30 seconds " );
After 20 minutes strtotime( " 20 minutes " );
12 hours later strtotime( " 12 hours " );
date function
The date() function formats a local time/date.
Grammar
date(format,timestamp)
date_default_timezone_set function
The date_default_timezone_set() function sets the default time zone used for all date/time functions in scripts.
date_default_timezone_set(timezone)
Example
The first case is that there is no database, and if you just compare the obtained date values, you have to use PHP’s time and date function to calculate it, as follows:
For example, to calculate how many days are left from 2015-9-5 to 2015-9-18:
<?php $startdate=strtotime("2015-9-5"); $enddate=strtotime("2015-9-18"); //上面的php时间日期函数已经把日期变成了时间戳,就是变成了秒。这样只要让两数值相减,然后把秒变成天就可以了,比较的简单,如下: $days=round(($enddate-$startdate)/3600/24) ; echo $days; //days为得到的天数; ?>
The second type Children’s growth
<? date_default_timezone_set('Asia/Shanghai'); //以上一句为设置时区,其实不设也行,但是zde debug的时候会有提示,说什么不安全的函数…添上吧。 echo date('Y-m-d H:i:s').' 今天是'.date('Y').'年的第'.date('W').'周'; $stime='2005-11-03 10:08'; echo "<br/><br/>***自出生(<font color=blue>$stime</font>)以来…:<br/><br/>"; echo "今天是第<font color=red><b>".Lnbsp(daysofnow($stime),3)."</b></font>天<br/>"; echo "今天是第<font color=red><b>".Lnbsp(weeksofnow($stime),3)."</b></font>周<br/>"; echo "今天是第<font color=red><b>".Lnbsp(monthsofnow($stime),3)."</b></font>个月<br/>"; echo "今天是第<font color=red><b>".Lnbsp(yearsofnow($stime),3)."</b></font>年<br/>"; /* $output=sprintf(" 今天是第<font color=red><b>%03d</b></font>天<br/>今天是第< font color=red><b>%03d</b></font>周<br/>今天是第< font color=red><b>%03d</b></font>个月<br/>今天是第< font color=red><b>%03d</b></font>年<br/& gt;",daysofnow($stime),weeksofnow($stime),monthsofnow($stime),yearsofnow($stime)); echo $output; */ function weeksofnow($stime) { $ftime=strtotime($stime); $fweeks=date('w',$ftime); if ($fweeks==0) $fweeks=7; $nweeks=date('w'); if ($nweeks==0) $nweeks=7; $ftemp=strtotime(date('Y-m-d 00:00:00',$ftime))-$fweeks*60*60*24; $ntemp=strtotime(date('Y-m-d 00:00:00',time()))+(7-$nweeks)*60*60*24; //echo date('w',$ftemp)."<br/>....<br/>".date('w',$ntemp)."<br/>"; return ($ntemp-$ftemp)/60/60/24/7; } function daysofnow($stime) { $ftime=strtotime($stime); return ceil(abs((time()-$ftime)/(60*60*24))); } function monthsofnow($stime) { $ftime=strtotime($stime); $fmonth=date('m',$ftime); $fyear=date('Y',$ftime); $nmonth=date('m'); $nyear=date('Y'); $result=($nyear-$fyear)*12+$nmonth-$fmonth+1; return $result; } function yearsofnow($stime) { $ftime=strtotime($stime); $fyear=date('Y',$ftime); $nyear=date('Y'); return $nyear-$fyear+1; } // 下面的函数只是加空格用的,不是核心内容,只为美观 function Lnbsp($data,$num) { $result=trim($data); for($i=$num;$i>=strlen($data);$i--) { $result=' '.$result; } return $result; } ?>
The third situation: For the dates of tomorrow, next month and next year, you can use the following code:
$tomorrow = date('Y-m-d',mktime (0,0,0,date("m"),date("d")+1,date("Y"))); $nextmonth = date('Y-m',mktime (0,0,0,date("m")+1,date("d")+1,date("Y"))); $nextyear = date('Y',mktime (0,0,0,date("m"),date("d"),date("Y")+1)); echo $tomorrow.'<br/>'; echo $nextmonth.'<br/>'; echo $nextyear.'<br/>';
The fourth situation: Working hours (excluding holidays)
<? $startDate="2001-12-12"; $endDate="2002-11-1"; $holidayArr=array("05-01","05-02","10-01","10-01","10-02","10-03","10-04","10-05","01-26","01-27","01-28","01-29"); //假期日期数组,比方国庆,五一,春节等 $endWeek=2; //周末是否双休.双休为2,仅仅星期天休息为1,没有休息为0 $beginUX=strtotime($startDate); $endUX=strtotime($endDate); for($n=$beginUX;$n<=$endUX;$n=$n+86400){ $week=date("w",$n); $MonDay=date("m-d",$n); if($endWeek){//去处周末休息 if($endWeek==2){ if($week==0||$week==6) continue; } if($endWeek==1){ if($week==0) continue; } } if(in_array($MonDay,$holidayArr)) continue; $totalHour+=10;//每天工作10小时 } echo "开始日期:$startDate<BR>"; echo "结束日期:$endDate<BR>"; echo "共花了".$totalHour."小时"; ?>
The fifth situation: give seconds to calculate hours
<?php function transform($sec){ $output = ''; $hours = floor($sec / 3600); $remainSeconds = $sec % 3600; $minutes = floor($remainSeconds / 60); $seconds = $sec - $hours * 3600 - $minutes * 60; if($sec >= 3600){ $output .= $hours.' h / '; $output .= $minutes.' m / '; } if($sec >= 60 && $sec < 3600){ $output .= $minutes.' m / '; } return $output .= $seconds.' s '; } echo transform(3231803); ?>

在使用PHP编写程序时,我们常常会需要将日期转换为时间戳,或者将时间戳转换为日期。这个过程在PHP中非常简单,只需要使用几个简单的函数就可以轻松地完成。在本文中,我们将介绍如何将PHP日期转换为时间戳,同时也会讨论一些常见的使用场景。

在PHP中,可以使用date()函数来获取当前日期和星期几。只需将date()函数的第二个参数省略,第一个参数设置为“Y-m-d H:i:s”即可获取当前日期,语法“date("Y-m-d H:i:s")”;而当第一个参数设置为“N”时则可获取是星期几,语法“date("N")”。“N”可返回ISO-8601格式数字表示的星期中的第几天,范围1(表示星期一)到7(表示星期天)。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

计算日期差值在实际应用中非常常见,比如在做网站开发时,我们可能需要计算两个日期之间的差值,以便正确地显示时间戳、倒计时等。而在计算日期差值时,我们经常需要计算两个日期的月份差值,下面我们来介绍如何用PHP计算日期差几个月。

在php中,可以使用date()函数来将时间戳转换为日期时间,该函数可以将时间戳格式化为可读性更好的日期和时间;语法格式“date(format,时间戳)”,参数format用于指定格式化字符,设置要转换成的日期格式,例如“Y-m-d H:i:s”,就是将时间戳转为“年-月-日 时:分:秒”格式。

PHP是一种流行的编程语言,广泛应用于Web开发。在PHP中,字符串和日期之间的转换是一个非常常见的操作。在这篇文章中,我们将讨论如何把PHP字符串转成时间格式。

在PHP开发中,经常会遇到将数字月份转换为英文月份的情况。这是很常见的需求,因为不同的国家和语言习惯使用不同的月份表示方式。比如,在美国和英国,人们通常使用英文月份名称表示时间,而在法国和德国,人们通常使用本地语言的月份名称。本文将介绍如何使用PHP将月份数字转换为英文月份名称。

在php中,可以使用date()函数来将秒数转为时分秒的格式,语法“date("H:i:s",时间戳)”。时间戳是自Unix纪元起到当前时间的秒数,而date()函数可以格式化时间戳,通过第一个参数设置的格式符将时间戳转为指定格式的日期字符串。格式符“H”表示24小时制,也可用“h”符,它表示12小时制;格式符“i”表示带前导零的分钟值,格式符“s”表示带前导零秒钟值。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download
The most popular open source editor

Dreamweaver Mac version
Visual web development tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 English version
Recommended: Win version, supports code prompts!
