convert the month number to date - php

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');

Related

Input a date and give a date in the future

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');

PHP: Incorrect strtotime()

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

add day to current date

add a day to date, so I can store tomorrow's date in a variable.
$tomorrow = date("Y-m-d")+86400;
I forgot.
date returns a string, whereas you want to be adding 86400 seconds to the timestamp. I think you're looking for this:
$tomorrow = date("Y-m-d", time() + 86400);
I'd encourage you to explore the PHP 5.3 DateTime class. It makes dates and times far easier to work with:
$tomorrow = new DateTime('tomorrow');
// e.g. echo 2010-10-13
echo $tomorrow->format('d-m-Y');
Furthermore, you can use the + 1 day syntax with any date:
$xmasDay = new DateTime('2010-12-24 + 1 day');
echo $xmasDay->format('Y-m-d'); // 2010-12-25
date() returns a string, so adding an integer to it is no good.
First build your tomorrow timestamp, using strtotime to be not only clean but more accurate (see Pekka's comment):
$tomorrow_timestamp = strtotime("+ 1 day");
Then, use it as the second argument for your date call:
$tomorrow_date = date("Y-m-d", $tomorrow_timestamp);
Or, if you're in a super-compact mood, that can all be pushed down into
$tomorrow = date("Y-m-d", strtotime("+ 1 day"));
Nice and obvious:
$tomorrow = strtotime('tomorrow');
You can use the add method datetime class.
Eg, you want to add one day to current date and time.
$today = new DateTime();
$today->add(new DateInterval('P1D'));
Further reference php datetime add
Hope this helps.
I find mktime() most useful for this sort of thing. E.g.:
$tomorrow=date("Y-m-d", mktime(0, 0, 0, date("m"), date("d")+1, date("Y")));

Better way of creating a PHP future date

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

php get future date time

I don't know how to explain this correctly but just some sample for you guys so that you can really get what Im trying to say.
Today is April 09, 2010
7 days from now is April 16,2010
Im looking for a php code, which can give me the exact date giving the number of days interval prior to the current date.
I've been looking for a thread which can solve or even give a hint on how to solve this one but I found none.
If you are using PHP >= 5.2, I strongly suggest you use the new DateTime object, which makes working with dates a lot easier:
<?php
$date = new DateTime("2006-12-12");
$date->modify("+7 day");
echo $date->format("Y-m-d");
?>
Take a look here - http://php.net/manual/en/function.strtotime.php
<?php
// This is what you need for future date from now.
echo date('Y-m-d H:i:s', strtotime("+7 day"));
// This is what you need for future date from specific date.
echo date('Y-m-d H:i:s', strtotime('01/01/2010 +7 day'));
?>
The accepted answer is not wrong but not the best solution:
The DateTime class takes an optional string in the constructor, which can define the same logic as the modify method.
<?php
$date = new DateTime("+7 day");
For example:
<?php
namespace DateTimeExample;
$now = new \DateTime("now");
$inOneWeek = new \DateTime("+7 day");
printf("Now it's the %s", $now->format('Y-m-d'));
printf("In one week it's the %s", $inOneWeek->format('Y-m-d'));
For a list of available relative formats (for the DateTime constructor) take a look at http://php.net/manual/de/datetime.formats.relative.php
If you are using PHP >= 5.3, this could be an option.
<?php
$date = new DateTime( "2006-12-12" );
$date->add( new DateInterval( "P7D" ) );
?>
You will have to look into strtotime(). I'd imagine your final code would look something like this:
$future_date = "April 16,2010";
$seconds = strtotime($future_date) - time();
$days = $seconds /(60 * 60* 24);
echo $days; //Returns "6.0212962962963"
You can use mktime with date. (http://php.net/manual/en/function.date.php)
Date gives you the current date. This is better than simply adding/subtracting to a timestamp since it can take into account daylight savings time.
<?php
# this gets you 7 days earlier than the current date
$lastWeek = mktime(0, 0, 0, date("m") , date("d")-7, date("Y"));
# now pretty-print it out (eg, prints April 2, 2010.)
echo date("F j, Y.", $lastWeek), "\n";
?>

Categories