Home >Backend Development >PHP Tutorial >Code to get the date of the day before yesterday and yesterday in PHP_PHP Tutorial
It was the same when I went for the interview the day before yesterday, but I couldn't remember it at the time. I just remember date_sub(now(),'interval 1 day'); date('Y/m/d h:i:s',mktime in MYSQL (date('h'), date('i'), date('s'), date('m') , date('d')+1, date('Y')));
----------------------------------------- ---------------------------------------
Get today's UNIXTIME first
Then subtract the seconds of one or two days
Format the subtracted UNIXTIME into a date.
-------------------------------------------------- ------------------------------------
CODE:[Copy to clipboard]date_default_timezone_set('Asia/Shanghai');
#Yesterday
echo date("Y/m/d h:i:s",time( )-24*60*60);
echo "
";
#Day before yesterday
echo date("Y/m/d h:i:s",time()-2* 24*60*60);
?>
-------------------------------- -------------------------------------------------- --
up
---------------------------------- ---------------------------------------------
The method is There are many kinds, let me introduce one:
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")));
---------------------------------- -----------------------------------------------
Calculating time used to be very annoying. Haha, I learned a lesson. Here is the current time next week.
date_default_timezone_set('Asia/Shanghai');
$tmp = time()+60*60*24*7;
print date("m/d/Y H:i:s", $ tmp);
----------------------------------------- ---------------------------------------
Add one more:
$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 Yesterday's
-------------------------- -------------------------------------------------- ---
Master Ruyunna
---------------------------------- --------------------------------------------------
QUOTE:
Original post by hutule110 on 2007-3-22 09:51
date("Y/m/d H:i:s" ,mktime(0,0,0,date("m"),date("d")-1,date("Y")));
This is great!
-------------------------------------------------- ------------------------------------
// Yesterday
print date('Y-m-d' , strtotime('-1 day'));
//Last week
print date('Y-m-d' , strtotime('-1 week'));
//Last month
print date('Y-m-d' , strtotime('-1 month'));
//Last year
print date('Y-m-d' , strtotime('-1 year'));
?>
------------------- -------------------------------------------------- -----------
QUOTE:
Original post by hutule110 on 2007-3-22 09:51
date("Y/ m/d H:i:s",mktime(0,0,0,date("m"),date("d")-1,date("Y")));
The number of times the function is called Got some
--------------------------------------------- -------------------------------------
strtotime gets a timestamp, Then format it yourself.
strtotime('yesterday');
strtotime('-2 day');