PHP日期时间函数的一些常用技巧

时间:2021-01-31 04:05:41

PHP日期时间函数的一些常用技巧整理

<?php
/*  [Author:lucas  Date:2007.11.15]  */

/********************************************************************/
//get a 10-digit date and time current timestamp
//it shows how manay seconds it has run since 1970-01-01
//要找出前一天的时间就是 time()-60*60*24
//要找出前一年的时间就是 time()-60*60*24*365

echo time();                            //[output: 119510414015]
echo date("Y-m-d",time()-60*60*24*365); //[output:2006-11-15]

/********************************************************************/
// set default timezone and get today's date
/* date("Y-m-d H:i:s",$t) used to format date,$t arg was a timestamp
第一个参数的格式分别表示:
a - "am" 或是 "pm" 
A - "AM" 或是 "PM" 
d - 几日,二位数字,若不足二位则前面补零; 如: "01" 至 "31" 
D - 星期几,三个英文字母; 如: "Fri" 
F - 月份,英文全名; 如: "January" 
h - 12 小时制的小时; 如: "01" 至 "12" 
H - 24 小时制的小时; 如: "00" 至 "23" 
g - 12 小时制的小时,不足二位不补零; 如: "1" 至 12" 
G - 24 小时制的小时,不足二位不补零; 如: "0" 至 "23" 
i - 分钟; 如: "00" 至 "59" 
j - 几日,二位数字,若不足二位不补零; 如: "1" 至 "31" 
l - 星期几,英文全名; 如: "Friday" 
m - 月份,二位数字,若不足二位则在前面补零; 如: "01" 至 "12" 
n - 月份,二位数字,若不足二位则不补零; 如: "1" 至 "12" 
M - 月份,三个英文字母; 如: "Jan" 
s - 秒; 如: "00" 至 "59" 
S - 字尾加英文序数,二个英文字母; 如: "th","nd" 
t - 指定月份的天数; 如: "28" 至 "31" 
U - 总秒数 
w - 数字型的星期几,如: "0" (星期日) 至 "6" (星期六) 
W - 计算一年中的第几周
Y - 年,四位数字; 如: "1999" 
y - 年,二位数字; 如: "99" 
z - 一年中的第几天; 如: "0" 至 "365" 
*/
date_default_timezone_set("PRC");
echo date("j F Y H:i:s");    //[output: 15 November 2007 13:20:58]

/********************************************************************/
/*The mktime function is forgiving if you supply it with nonsense arguments,
such as a day of the month that doesn't exist. For instance, if you try to
calculate a timestamp for February 29 in a non-leap year, the value returned
will actually represent March 1 .*/
echo date("d/m/Y", mktime(12, 0, 0, 2, 29, 2003));//[output:01/03/2003]
echo date("d/m/Y H:i:s",mktime(12 + 37, 0, 0, 12, 30, 2001));//[output:01/01/2002 01:00:00]

/********************************************************************/
//using explode function to make timestamp
$date = "08-30-2007";
$parts = explode("-", $date);
$timestamp = mktime(12, 0, 0,$parts[0], $parts[1], $parts[2]);
echo date("d/m/Y",$timestamp);    //[output:30/08/2007]

/********************************************************************/
//using strtotime function to make timestamp
$timestamp = strtotime("3 May 04");
//$timestamp = strtotime("3rd May 2004");
//$timestamp = strtotime("May 3, 2004");
//$timestamp = strtotime("3-may-04");
//$timestamp = strtotime("2004-05-03");
echo date("d/m/Y",$timestamp);      //[output:03/05/2004]

/********************************************************************/
//judge today whether is weekend
$now = getdate();
switch ($now[wday]) {
  case 0:  // Sunday
  case 6:  // Saturday
           echo "It's the weekend";
           break;
  default: echo "It's a weekday";
}

/********************************************************************/
//using ereg_replace to format a date

$date = "08/26/2003";
print ereg_replace("([0-9]+)/([0-9]+)/([0-9]+)","
//2///1///3",$date);  //[output:26/08/2003]

/********************************************************************/
?>