Say I have code: date('Y')."-".date('m')."-".date('d') How would I go about removing a number of years from the date that piece of code gives me?
$date = strtotime('-2 years');
And since your printing code is nothing but a convoluted version of this:
echo date('Y-m-d', $date);
... you could simplify everything into a one-liner:
echo date('Y-m-d', strtotime('-2 years'));
For your situation you could take advantage of PHP's implicit type casting and simply use
(date('Y')-42)."-".date('m')."-".date('d')
or shorter
(date('Y')-42)."-".date('m-d')
But, as Álvaro G. Vicario observed in a comment, there is the dreaded 29th of February that doesn't exist in all years so you'll have to use strtotime
Example:
$someDay = strtotime('2009-11-23');
$threeYearsBefore = strtotime('-3 years', $someDay);
echo date('Y-m-d', $threeYearsBefore);
or, relative to the current time,
$threeYearsBefore = strtotime('-3 years');
echo date('Y-m-d', $threeYearsBefore);
As of PHP5.2 you can use the DateTime class along with DateInterval to accomplish as well:
$now = new DateTime();
$two_years_ago = $now->sub(new DateInterval("P2Y"));
echo $two_years_ago->format('Y-m-d');
echo date("Y-m-d", strtotime('-20 years'));
Related
I had gone through various stackoverflow solutions and other blogs but still it doesn't fix my problem.
Let's say that the date today is: 2013-12-28 and I want to get the date after 1 month and it is supposed to display 2014-01-28.
$date = date('o-m-d');
$final = date('o-m-d', strtotime("+1 month", $date));
echo $final;
Above is my code. It returns 02/01/1970.
I have also tried the mktime method but still it displays the 1970 output.
What am I doing wrong?
BTW. I am working this on a hosted server.
Thanks ahead. :)
Use DateTime function modify
$date = new DateTime( 'o-m-d' );
echo $date->modify( '+1 month' )->format('o-m-d');
If you want the current date +1 month use:
$final = date('o-m-d', strtotime("+1 month"));
Or with a given date:
$date = date('o-m-d');
$final = date('o-m-d', strtotime($date . " +1 month"));
echo $final;
If you want to use the second parameter of strtotime it has to be a timestamp.
Go the OOP way..
<?php
$date = new DateTime('2013-12-28');
$date->add(new DateInterval('P1M'));
echo $date->format('Y-m-d'); //prints 2014-01-28
<?php
$date = "04-15-2013";
$date = strtotime($date);
$date = strtotime("+1 day", $date);
echo date('m-d-Y', $date);
?>
This is driving me crazy and seems so simple. I'm pretty new to PHP, but I can't figure this out. The echo returns 01-01-1970.
The $date will be coming from a POST in the format m-d-Y, I need to add one day and have it as a new variable to be used later.
Do I have to convert $date to Y-m-d, add 1 day, then convert back to m-d-Y?
Would I be better off learning how to use DateTime?
there you go
$date = "04-15-2013";
$date1 = str_replace('-', '/', $date);
$tomorrow = date('m-d-Y',strtotime($date1 . "+1 days"));
echo $tomorrow;
this will output
04-16-2013
Documentation for both function
date
strtotime
$date = DateTime::createFromFormat('m-d-Y', '04-15-2013');
$date->modify('+1 day');
echo $date->format('m-d-Y');
See it in action
Or in PHP 5.4+
echo (DateTime::createFromFormat('m-d-Y', '04-15-2013'))->modify('+1 day')->format('m-d-Y');
reference
DateTime::createFromFormat()
$date = strtotime("+1 day");
echo date('m-d-y',$date);
use http://www.php.net/manual/en/datetime.add.php like
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('1 days'));
echo date_format($date, 'Y-m-d');
output
2000-01-2
The format you've used is not recognized by strtotime(). Replace
$date = "04-15-2013";
by
$date = "04/15/2013";
Or if you want to use - then use the following line with the year in front:
$date = "2013-04-15";
Actually I wanted same alike thing,
To get one year backward date, for a given date! :-)
With the hint of above answer from #mohammad mohsenipur
I got to the following link, via his given link!
Luckily, there is a method same as date_add method, named date_sub method! :-)
I do the following to get done what I wanted!
$date = date_create('2000-01-01');
date_sub($date, date_interval_create_from_date_string('1 years'));
echo date_format($date, 'Y-m-d');
Hopes this answer will help somebody too! :-)
Good luck guys!
I want to get future date after specific date
for this i use strtotime() function but it not work for me
then I use following code
$d1='2012-11-08';
$d2=date($d1, strtotime('+3 days'));
echo $d2;
output is
2012-11-08
2012-11-08
not
2012-11-11
but output is not 2012-11-11
output is 2012-11-08
i can't solve this what is error i do and how i solve this?
$d1='2012-11-08';
$d2=new DateTime($d1);
$d2->modify('+3 day');
echo $d2->format('Y-m-d');
Try using date_add
$d1 = '2012-11-08';
$d2 = date_add($d1, date_interval_create_from_date_string('3 days'));
echo $d2;
http://www.php.net/manual/en/datetime.add.php
If you're not running PHP 5.3, this should work:
$d1 = '2012-11-08';
$d2 = strtotime('+3 days', strtotime($d1));
echo date('Y-m-d', $d2);
You are using the date() function incorrectly. As per the documentation, date() takes a format string followed by an optional timestamp. You're giving it a date string and another date.
You can do what you want like this, where strtotime is used to modify the date,
$date = "2012-11-08";
echo date("Y-m-d", strtotime($date. " + 3 days"));
But if you're running PHP 5.2+ you should probably use the DateTime class, as it's got much better date handling, and it's easier to see what's going on with it.
$datetime = new DateTime("2012-11-08");
$datetime->modify("+ 3 days");
echo $datetime->format("Y-m-d");
Actually you wrongly added the strtotime(), it works when you use current date, if you want to add with customize date, You can try this,
<?php
$d1='2012-11-08';
$d2 = strtotime ( '+3 day' , strtotime ( $d1 ) ) ;
$d3 = date ( 'Y-m-d' , $d2);
?>
if you want to add date from current date, use the following,
$d1 = Date('Y-m-d', strtotime("+3 days"));
Whilst strtotime() is a handy tool, it is prone to locale issues.
I would instead use PHP's mature DateTime class, eg
$dt = new DateTime($d1);
echo $dt->add(new DateInterval('P3D'))->format('Y-m-d');
Given a time, how can I find the time one month ago.
strtotime( '-1 month', $timestamp );
http://php.net/manual/en/function.strtotime.php
In php you can use strtotime("-1 month"). Check out the documentation here: http://ca3.php.net/strtotime
We can achieve same by using PHP's modern date handling. This will require PHP 5.2 or better.
// say its "2015-11-17 03:27:22"
$dtTm = new DateTime('-1 MONTH', new DateTimeZone('America/Los_Angeles')); // first argument uses strtotime parsing
echo $dtTm->format('Y-m-d H:i:s'); // "2015-10-17 03:27:22"
Hope this adds some more info for this question.
<?php
$date = new DateTime("18-July-2008 16:30:30");
echo $date->format("d-m-Y H:i:s").'<br />';
date_sub($date, new DateInterval("P1M"));
echo '<br />'.$date->format("d-m-Y").' : 1 Month';
?>
PHP 5.2=<
$date = new DateTime(); // Return Datetime object for current time
$date->modify('-1 month'); // Modify to deduct a month (Also can use '+1 day', '-2 day', ..etc)
echo $date->format('Y-m-d'); // To set the format
Ref: http://php.net/manual/en/datetime.modify.php
This code is for getting 1 month before not 30 days
$date = "2016-03-31";
$days = date("t", strtotime($date));
echo date("Y-m-d", strtotime( "-$days days", strtotime($date) ));
These answers were driving me nuts. You can't subtract 31 days and have a sane result without skipping short months.
I'm presuming you only care about the month, not the day of the month, for a case like filtering/grouping things by year and month.
I do something like this:
$current_ym = date('ym',strtotime("-15 days",$ts));
I've got a date in this format:
2009-01-01
How do I return the same date but 1 year earlier?
You can use strtotime:
$date = strtotime('2010-01-01 -1 year');
The strtotime function returns a unix timestamp, to get a formatted string you can use date:
echo date('Y-m-d', $date); // echoes '2009-01-01'
Use strtotime() function:
$time = strtotime("-1 year", time());
$date = date("Y-m-d", $time);
Using the DateTime object...
$time = new DateTime('2099-01-01');
$newtime = $time->modify('-1 year')->format('Y-m-d');
Or using now for today
$time = new DateTime('now');
$newtime = $time->modify('-1 year')->format('Y-m-d');
an easiest way which i used and worked well
date('Y-m-d', strtotime('-1 year'));
this worked perfect.. hope this will help someone else too.. :)
On my website, to check if registering people is 18 years old, I simply used the following :
$legalAge = date('Y-m-d', strtotime('-18 year'));
After, only compare the the two dates.
Hope it could help someone.
// set your date here
$mydate = "2009-01-01";
/* strtotime accepts two parameters.
The first parameter tells what it should compute.
The second parameter defines what source date it should use. */
$lastyear = strtotime("-1 year", strtotime($mydate));
// format and display the computed date
echo date("Y-m-d", $lastyear);
Although there are many acceptable answers in response to this question, I don't see any examples of the sub method using the \Datetime object: https://www.php.net/manual/en/datetime.sub.php
So, for reference, you can also use a \DateInterval to modify a \Datetime object:
$date = new \DateTime('2009-01-01');
$date->sub(new \DateInterval('P1Y'));
echo $date->format('Y-m-d');
Which returns:
2008-01-01
For more information about \DateInterval, refer to the documentation: https://www.php.net/manual/en/class.dateinterval.php
You can use the following function to subtract 1 or any years from a date.
function yearstodate($years) {
$now = date("Y-m-d");
$now = explode('-', $now);
$year = $now[0];
$month = $now[1];
$day = $now[2];
$converted_year = $year - $years;
echo $now = $converted_year."-".$month."-".$day;
}
$number_to_subtract = "1";
echo yearstodate($number_to_subtract);
And looking at above examples you can also use the following
$user_age_min = "-"."1";
echo date('Y-m-d', strtotime($user_age_min.'year'));