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

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]

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值