简体   繁体   中英

php date usage for today and yesterday

I am a beginner for php. I am trying to integrate an XML API to my system. Calling code is

$XML = simplexml_load_file('http://my.mydomain.com/stats/report.xml?api_key=XXXXXXXX&start_date='.date('Y-m-d').'&end_date='.date('Y-m-d'));

What I need to do is;

  • start_date = yesterday
  • end_date = today

I could not find how to describe.

You can simply use strtotime() call like so:

$today = date('Y-m-d', strtotime('today') );
$yesterday = date('Y-m-d', strtotime('1 day ago') );
$Start_Date=date('d.m.Y',strtotime("-1 days"));
$End_Date = date('d.m.y', strtotime('today') );

PHP strtotime() function helps in this:

You could use

$XML = simplexml_load_file('http://my.mydomain.com/stats/report.xml?api_key=XXXXXXXX&start_date='.date('Y-m-d',strtotime('yesterday')).'&end_date='.date('Y-m-d',strtotime('yesterday'));

Yes its that simple, date('Ym-d',strtotime('yesterday')) gives you yesterday's date !!

ref: http://php.net/manual/en/function.strtotime.php

int strtotime ( string $time [, int $now = time() ] )

$time = A date/time string. Valid formats are explained in Date and Time Formats.

$now = This is an optional parameter for the timestamp which is used as a base for the calculation of relative dates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM