I am letting a user input a date. How should I return a date from their input and add 3 years to it? I tried to use mktime and date but it did not work out very well.
$input_date = 2010-03-28
My solution currently is just basic math for a given date 3 years ahead.
$input_date = $input_date + 3000;
Let's say I would want to give a date 4 years and 4 months 10 days
$future_date1 _datum = mktime(0, 0, 0, date("m")-2, date("d"), date("Y")+3);
$future_date2 = mktime(0, 0, 0, date("m"), date("d"), date("Y")+3);
You can use strtotime('+ 3 years') or DateInterval for an object oriented approach.
Use the strtotime function:
$input_date = '2010-03-28';
$future_date = strtotime("+3 years", strtotime($input_date);
http://www.php.net/manual/en/function.strtotime.php
Returns a timestamp, if you want to return YYYY-mm-dd: $future_date = date("Y-m-d", $future_date);
DateTime() offers multiple ways to do this. It's the way PHP recommends doing date math.
You can use DateTime::modify():
$date = new DateTime($input_date);
$date->modify('+3 months');
echo $date->format('Y-m-d');
// one-liner
echo (new DateTime($input_date))->modify('+3 months')->format('Y-m-d');
or with DateInterval()
$date = new DateTime($input_date);
$date->add(new DateInterval('P3M'));
echo $date->format('Y-m-d');
// one-liner
echo (new DateTime($input_date))->add(new DateInterval('P3M'))->format('Y-m-d');
Related
This is what am trying to get. someone hired an item on 2018-09-09 for 45 days. how will i convert this 45 days to the nearest datetime judging from the date of purchase(2018-09-09).
That is I need to get the datetime of 45 days with 2018-09-09 as a starting point.
Is this obtainable. This is what I have tried
$today = '2018-09-09';
$days = 45;
$year = 2018;
echo date('Y-m-d', mktime( 0, 0, 0, 1, $days, $year));
Like this
echo (new DateTime('2018-09-09'))->modify('+45 days')->format('Y-m-d');
Output
2018-10-24
Sandbox
If you wish to add a pre-defined amount of days to a specific date ( which is kind of how I interpreted the question ) then you might look at using the DateTime class with it's associated methods, for instance:
$now = new DateTime('2018-09-09');
/* http://www.php.net/manual/en/class.dateinterval.php */
$period = new DateInterval('P45D');
/* http://www.php.net/manual/en/datetime.add.php */
$future = $now->add( $period );
echo $future->format('y-m-d');
Will print:
18-10-24
Use Datetime and Dateinterval:
$today = '2018-09-09';
$datetime = new DateTime($today);
$dateinterval = new DateInterval('P45D');
$datetime->add($dateinterval);
echo $datetime->format('Y-m-d');
I have a date which is say like this
$given_date = '2014-12-25'; //Y-m-d format
Now i want to get the midnight timestamp of the given date, so I am doing this method
$midnight = strtotime(date('Y-m-d',$given_date).' 00:00:00');
Am I doing it right??
or I can use something like this?
$midnight = strtotime("midnight $given_date");
Which is better?
I would prefer a more OO approach instead of fiddling around with strings:
$date = new DateTime($given_date);
$date->setTime(0,0,0);
// echo $date->format('Y-m-d H:i:s');
echo $date->getTimestamp();
Using the static method createFromFormat from DateTime you can force the time-parts to be reset to 0 using '|':
$date = DateTime::createFromFormat('Y-m-d|', $given_date);
echo $date->format('Y-m-d H:i:s');
It is also possible to do it with:
list($y, $m, $d) = explode('-', $given_date);
$midnight = mktime(0, 0, 0, $m, $d, $y);
I am working on a task where I need the user to provide exact number of months it will take them to complete a task and then i need to convert that number to an exact date, so lets suppose a user enters 6, this should give me a date 6 months from now.
I tried the following code looking at different examples on line but I have a feeling the following examples treats the $monthNum as the actual month of a year rather than what I need it to do.
$monthNum = 5;
$monthName = date("F", mktime(0, 0, 0, $monthNum, 10));
echo $monthName;
I will really appreciate any assistance here.
Demo here
Pop in your month in the modify() method.
$monthNum = 6;
$date = new DateTime();
$date->modify(" +{$monthNum} month");
echo $date->format("Y-m-d");
Outputs
2015-05-14
You can try:
$time = new \DateTime('+5 months');
You can use strtotime:
$date = date("Y-m-d", strtotime("+5 months"));
in php 5.4+
echo (new DateTime())->modify('+6 months')->format('d M Y');
Is there a quicker way of creating a date such as:
echo date('Y-m-d', mktime(0, 0, 0, date("m"), date("d")+3, date("Y")));
Thanks if you can help.
How about strtotime():
date('Y-m-d', strtotime('+3 days'));
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$currentDate = strtotime('today');//your date variable goes here
$futureDate = date('Y-m-d', strtotime('+ 2 days', $currentDate));
echo $futureDate;
Live Demo
If you are using PHP version >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$futureDate = new DateTime("today");
$futureDate->modify("+2 days");
echo $futureDate->format("Y-m-d");
Live Demo
I've rarely touched PHP date functions,
and now I need to do the follows:
get current date,
get date of three days later
get date of three weeks later
get date of three month later
get date of three years later
and finally to implement such a function:
function dage_generate($number,$unit)
{
}
$unit can be day/week/month/years
http://uk.php.net/strtotime can do most of that:
strtotime("today")
strtotime("+ 3 days")
strtotime("+ 3 weeks")
strtotime("+ 3 months")
strtotime("+ 3 years")
The function would be something like:
function dage_generate($number,$unit)
{
return strtotime("+ ".$number." ".$unit);
}
http://us.php.net/manual/en/function.date.php
Note towards the bottom of the page:
Example #3 date() and mktime() example
<?php
$tomorrow = mktime(0, 0, 0, date("m") , date("d")+1, date("Y"));
$lastmonth = mktime(0, 0, 0, date("m")-1, date("d"), date("Y"));
$nextyear = mktime(0, 0, 0, date("m"), date("d"), date("Y")+1);
?>
Using strtotime() you can do this easily.
$now = time();
$threeDays = strtotime("+3 days");
$threeWeeks = strtotime("+3 weeks");
$threeMonths = strtotime("+3 months");
$threeYears = strtotime("+3 years");
Each of those variables will be an integer which represents the unix timestamp at that point in time. You can then format it into a human readable string using date().
echo date('r', $threeWeeks);
// etc...
Use date_create() to create a DateTime object, then use the add method.
See the examples on the page for the add() method, they contain all you need.
PHP Date/Time Functions with examples