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');
Related
// to simplify $timestamp in this example is the unix timestamp of 2016-04-20
Consider this example:
strtotime('+1 year', $timestamp); // this returns 2017-04-19
How can I make it return 2017-04-20?
Another example:
strtotime('+1 month', $timestamp); // this returns 2016-05-19
How can I make it return 2016-05-20?
Basically, I want to relatively add time that ends up with the same date.
strtotime('+1 day', strtotime('+1 year', $timestamp));
?
$date = date("Y",$timestamp) + 1 //gives you the next year
$date .= "-" . date("m-d",$timestamp) //concantenates on the current month and day
I may be misunderstanding what you're asking but you're probably better of using the DateTime library built into PHP, it's a lot more flexible than the standard date() function.
So you could do:
$d = new DateTime();
$d->modify('+1 year');
echo $d->format('Y-m-d'); // Outputs: 2017-04-20
If you want to create a DateTime object from a specific date you can do so by:
$d = DateTime::createFromFormat('Y-m-d', '2016-01-01');
echo $d->format('Y-m-d'); // Outputs 2016-01-01
I believe that's what you're after, it's much cleaner than date() and easier to read in my personal opinion.
Im trying to add a certain amount of days to a timestmp using this in PHP:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo $endDate2;
but its displaying: 1216526400
any ideas?
Try:
echo date("Y-m-d H:i:s",$endDate2);
Or (for just the date):
echo date("Y-m-d",$endDate2);
You can find documentation about how to format your string here: http://php.net/manual/en/function.date.php
You should be using DateTime for working with dates. It's timezone friendly.
$datetime = new DateTime('2008-06-20');
$datetime->modify('+1 day');
echo $datetime->getTimestamp();
strtotime() converts the date into a unix timestamp which is the number of seconds since January 1st 1970. If you want a date output you have to run the finished timestamp through date() first.
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate.' +1 day');
echo date("Y-m-d", $endDate);
strtotime creates a Unix timestamp so if you want to be presented with a formatted date, you need to pass the timestamp as an argument to the date function as follows:
$capturedDate = '2008-06-20';
$endDate = strtotime($capturedDate);
$endDate2 = strtotime('+1 day',$endDate);
echo date('Y-m-d', $endDate2);
Additionally, there are a wide variety of parameters you can use in the date function if you want to display additional information.
e.g.: echo date('Y-m-d H:i:s', $endDate2); or echo date('Y-m-d h:i:s a', $endDate2);, etc.
Sooooo close, just take your timestamp and convert it back into date format using date("desired format",$endDate2);
DateTime is a very nice way to deal with dates. You can try like this:
$capturedDate = '2008-06-20';
$date = DateTime::createFromFormat('Y-m-d', $capturedDate)->modify('+1 day');
echo $date->getTimestamp();
<?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!
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'));
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));