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

Dani AI

Generated

The cleanest way is to work with PHP’s date/time objects, then format to the string you need. Using an immutable object also makes it harder to accidentally mutate your “now” value.

<?php
// Pick an explicit timezone so results are predictable
$tz = new DateTimeZone('UTC'); // or your app's TZ, e.g. 'America/New_York'

$today   = new DateTimeImmutable('today', $tz);       // midnight today in $tz
$back15  = $today->sub(new DateInterval('P15D'));     // subtract 15 days

$currentdate = $today->format('Y-m-d');
$dateback    = $back15->format('Y-m-d');

If you got the “Object of class DateTime could not be converted to string” error mentioned later in the thread, that is because you tried to echo the object directly. As and @diafol hint at below, always call ->format('Y-m-d') (or a related formatter) on the object before output or storage. If you are on older PHP and do not have DateTimeImmutable, you can swap it for DateTime with the same calls. See the official docs for DateTimeImmutable and DateInterval.

Shortcut for simple cases: date('Y-m-d', strtotime('-15 days')) also works, but be mindful it uses the server’s default timezone unless you set one. For reliability across hosts, either set date_default_timezone_set(...) early in your app or pass a DateTimeZone as shown above. Reference: strtotime.

Recommended Answers

All 9 Replies

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 Member #120589

@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.