分享下PHP获取时间日期的多种方法。
<?php
echo "今天:".date("Y-m-d")."<br>";     
echo "昨天:".date("Y-m-d",strtotime("-1 day")), "<br>";     
echo "明天:".date("Y-m-d",strtotime("+1 day")). "<br>";   echo "一周后:".date("Y-m-d",strtotime("+1 week")). "<br>";     
echo "一周零两天四小时两秒后:".date("Y-m-d G:H:s",strtotime("+1 week 2 days 4 hours 2 seconds")). "<br>";     
echo "下个星期四:".date("Y-m-d",strtotime("next Thursday")). "<br>";     
echo "上个周一:".date("Y-m-d",strtotime("last Monday"))."<br>";     
echo "一个月前:".date("Y-m-d",strtotime("last month"))."<br>";     
echo "一个月后:".date("Y-m-d",strtotime("+1 month"))."<br>";     
echo "十年后:".date("Y-m-d",strtotime("+10 year"))."<br>";    
strtotime()函数的作用是将日期时间描述解析为 Unix 时间戳
int strtotime ( string time [, int now] )
//from www.jbxue.com 脚本学堂
?>
本函数预期接受一个包含美国英语日期格式的字符串并尝试将其解析为 Unix 时间戳(自 January 1 1970 00:00:00 GMT 起的秒数),其值相对于 now 参数给出的时间,如果没有提供此参数则用系统当前时间。
在PHP里得到前天和昨天的日期的代码
前天去面试的时候也是这样,不过我当时记不起来了.就记得MYSQL里面的date_sub(now(),'interval 1 day');date('Y/m/d h:i:s',mktime(date('h'), date('i'), date('s'), date('m') , date('d')+1, date('Y')));
-------------------------------- 
先得到今天的UNIXTIME 
然后减去一天或两天的秒数 
把减后的UNIXTIME格式化成日期。 
<?php 
date_default_timezone_set('Asia/Shanghai'); 
#昨天 
echo date("Y/m/d h:i:s",time()-24*60*60); 
echo "<br>"; 
#前天 
echo date("Y/m/d h:i:s",time()-2*24*60*60); 
?> 
方法有很多种啊, 我也介绍一种吧: 
date("Y/m/d H:i:s", strtotime("1 days ago")); 
date("Y/m/d H:i:s", strtotime("2 days ago")); 
-------------------------------------------------------------------------------- 
date("Y/m/d H:i:s",mktime(0,0,0,date("m"),date("d")-1,date("Y"))); 
-------------------------------------------------------------------------------- 
以前算时间总是很烦人,呵呵,学了了下,下面是下个星期现在的时间。 
date_default_timezone_set('Asia/Shanghai'); 
$tmp = time()+60*60*24*7; 
print date("m/d/Y H:i:s", $tmp); 
再加一个: 
$time_yes=localtime(time()-24*60*60, true); 
$time_b_yes=localtime(time()-2*24*60*60, true); 
$yesterday=$time_yes['tm_mday']; 
$the_day_before_yes=$time_b_yes['tm_mday']; 
time()-86400 昨天的
<? 
//昨天 
print date('Y-m-d' , strtotime('-1 day')); 
//上星期 
print date('Y-m-d' , strtotime('-1 week')); 
//上个月 
print date('Y-m-d' , strtotime('-1 month')); 
//去年 
print date('Y-m-d' , strtotime('-1 year')); 
?> 
 本文出处参考:http://www.jbxue.com/article/10544.html

PHP获取时间日期的多种方法的更多相关文章

  1. WPF 获取系统 DPI 的多种方法

    原文:WPF 获取系统 DPI 的多种方法 WPF 获取系统 DPI 的多种方法 由于 WPF 的尺寸单位和系统的 DPI 相关,我们有时需要获取 DPI 值来进行一些界面布局的调整,本文汇总了一些 ...

  2. 体温数据上传程序开发+获取时间的三种方法+DB Browser下载及安装

    今天开始了体温上传程序的开发 今日所学: 获取时间 (21条消息) (转)安卓获取时间的三种方法_sharpeha的博客-CSDN博客_安卓获取时间 DB Browser安装教程 (20条消息) sq ...

  3. c#.net 获取时间日期年月日时分秒格式

    今天写代码发现两个比较不错的分享下:1.DateTime.ParseExact很多时候我们获取的时间是数字形式表示的,好比20140127134015.927856,通过这个方法DateTime.Pa ...

  4. c#.net 获取时间日期年月日时分秒生成自动文件名格式

    下面是日期和时间的各种方法,转换为字符串. 如果把输出的格式改下就可以做类似的文件名了,例如:2016010110101224356.doc  c#用DateTime.Now.ToString(&qu ...

  5. asp.net获取时间日期插入数据库

    //获取日期+时间 DateTime.Now.ToString(); // 2008-9-4 20:02:10 DateTime.Now.ToLocalTime().ToString(); // 20 ...

  6. Oracle获取时间日期月份星期数

    1.日期和字符转换函数用法(to_date,to_char)select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') as nowTime from dual; ...

  7. Shell获取字符串长度的多种方法总结

    摘自:https://www.jb51.net/article/121290.htm 前言 我们在日常工作中,对于求字符串操作在shell脚本中很常用,实现的方法有很多种,下面就来给大家归纳.汇总了求 ...

  8. bash shell脚本之获取时间日期

    shell中的时间日期获取 cat test5: #!/bin/bash # using the backtick character testing=`date` echo "The da ...

  9. Python中对时间日期的处理方法简单汇总

    这篇文章主要介绍了Python实用日期时间处理方法汇总,本文讲解了获取当前datetime.获取当天date.获取明天/前N天.获取当天开始和结束时间(00:00:00 23:59:59).获取两个d ...

随机推荐

  1. Balanced Lineup 倍增思想到ST表RMQ

      Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36864   Accepted: 172 ...

  2. structs 2 学习总结(一)

      转发请注明出处 1.页面传值. 传值 前台 <s:form action="login1">//action 名字 需要在structs配置 用户<s:tex ...

  3. Handler发送消息

    Handler发送消息小结 字数283 阅读210 评论0 喜欢1 obtainMessage()得到一个Message对象. 创建一个Message然后发送是这么写的: Message msg = ...

  4. Windows OpenVPN Client and tls-auth

    The official Windows OpenVPN client does not seem to work properly with the tls-auth option if a key ...

  5. SQL SERVER – Count Duplicate Records – Rows

    SELECT YourColumn, COUNT(*) TotalCount FROM YourTable GROUP BY YourColumn HAVING COUNT(*) > 1 ORD ...

  6. jquery plugins —— datatables ajax post更新数据

    通过下面语句,可以定义datatables插件通过ajax post方法从服务器段获取JSON格式的数据. 错误写法(这样写再执行ajax.reload()方法时,ID参数还是初始时,不会更新): v ...

  7. HDU 1069 Monkey and Banana (DP)

    Monkey and Banana Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u S ...

  8. Xcode6编译SDWebImage报错解决方法(SDWebImageDownloaderOperation.m错误)

    报错:Use of undeclared identifier '_executing' / '_finished': 解决方法: 在SDWebImageDownloaderOperation类的实现 ...

  9. 【转】亿欧盘点:杭州十家代表性O2O企业

    [ 亿欧导读 ] 11月13日亿欧网将走入杭州,联合B座12楼.正和岛召开“2014 中国O2O新商业峰会“.亿欧网据O2O产业图谱,整理出杭州十家O2O企业:点我吧.快的打车.杭州19楼.婚礼纪.淘 ...

  10. HttpContext 讲解

    HttpContext类:封装有关个别HTTP请求的所有HTTP特定的信息,又叫上下文.看到这个解释,我觉得有些抽象,Http特定信息具体又是什么?看了下备注:为继承 IHttpModule 和 IH ...