在PHP里得到前天和昨天的日期的代码

yipeiwu_com7年前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格式化成日期。

--------------------------------------------------------------------------------




CODE:[Copy to clipboard]<?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);
?>


--------------------------------------------------------------------------------

up

--------------------------------------------------------------------------------
方法有很多种啊, 我也介绍一种吧: 

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  昨天的

--------------------------------------------------------------------------------

高手如云那

--------------------------------------------------------------------------------




QUOTE:
原帖由 hutule110 于 2007-3-22 09:51 发表
date("Y/m/d H:i:s",mktime(0,0,0,date("m"),date("d")-1,date("Y"))); 
这个好啊!

--------------------------------------------------------------------------------

<?
//昨天
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'));
?>

--------------------------------------------------------------------------------



QUOTE:
原帖由 hutule110 于 2007-3-22 09:51 发表
date("Y/m/d H:i:s",mktime(0,0,0,date("m"),date("d")-1,date("Y"))); 
调用函数次数 多了些

--------------------------------------------------------------------------------

strtotime得到一个时间戳, 然后你自己格式化.
strtotime('yesterday');
strtotime('-2 day');

相关文章

PHP排序算法系列之直接选择排序详解

直接选择排序 直接选择排序(Straight Select Sorting) 的基本思想是:第一次从R[0]~R[n-1]中选取最小值,与R[0]交换,第二次从R[1]~R[n-1]中选...

php mssql 时间格式问题

当然是在程序里解决比较灵活,例如: “select convert(char,日期字段,120) as date2 from table” convert(char,date,N)输出的...

php 数组动态添加实现代码(最土团购系统的价格排序)

核心代码如下:复制代码 代码如下:<?php $now=time(); $oc = array( 'team_type' => 'normal', "begin_time &...

一个基于PDO的数据库操作类

百度之后决定使用PDO,至于为什么选择PDO,这里就不再多说,大家自己去百度下就能明白。 既然要换,那最基本就需要有个常用的数据库操作类,也就是所谓的增删改查等,昨晚捣腾了一晚,大致弄出...

PHP中array_merge和array相加的区别分析

首先来看看键名是string,两者区别: 复制代码 代码如下:<?php$arr1 = array('a'=>'PHP');$arr2 = array('a'=>'JA...