How I can get current date and 15 days back date from current date in 'Y-m-d' format ??

e.g. if current date is 2011-06-21 then $currentdate=2011-06-21 and $dateback=2011-06-06;

Abhishek_45 commented: $days_ago = date('Y-m-d', strtotime('-15 days', strtotime(date('Y-m-d')))); +0
time()
date_create('15 days ago')
time()
date_create('15 days ago')

I need complete code that store currentdate as well as backdate..

Well, I suggest you learn PHP first? Storing values in a variable is quite well documented :)

$now = time();
$fifteendaysago = date_create('15 days ago');
commented: date_create new for me - thanks :) +13

Well, I suggest you learn PHP first? Storing values in a variable is quite well documented :)

$now = time();
$fifteendaysago = date_create('15 days ago');

showing following error :

Catchable fatal error: Object of class DateTime could not be converted to string in C:\xampp\htdocs\3g\b.php on line 5

That's only if you echo it. Use $fifteendaysago->format(DATE_RFC3339) or something like that.

Member Avatar for diafol

@A

Did you check the php.net manual for date_create()? If you'd bothered, you'd have seem that your can't print it out directly.

echo date_format($fifteendaysago, 'Y-m-d');

Also you must be using 5.2+

BTW - cheers twiss, always nice to learn new things. Must have missed that one.

Well, didn't know it either, just searched the manual for it :)

$days_ago = date('Y-m-d', strtotime('-15 days', strtotime(date('Y-m-d'))));

Why do people answer the question seven years ago?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.