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');
Related
I'am writing a function in laravel that calculates the end date given the start date and no. of days and stores it in the database.
This is what I have come up with:
public function store(Request $request)
{
$it = new iternary;
$it->StartDate = Input::get("StartDate");
$it->NoPeople = Input::get("NoPeople");
$it->NoDays = Input::get("NoDays");
(int)$ttemp=$it->NoDays;
$it->budget = Input::get("budget");
$EndDate = date('Y-m-d', mktime(0, 0, 0, date('m'), date('d') + $it-
>NoDays, date('Y')));
$it->EndDate = $EndDate;
$it->save();
return view('success');
}
But this gives me the wrong output.
It gives me end date same as start date.
Can you'll help please.
Thanks
You can do it like this,
$startDate = new \Carbon\Carbon('2017-05-09')
$endDate = $startDate->addDays('10')
Further info about adding and subtracting in carbon can be found here here
You can also use PHP's default DateTime class for modification,
$date = new DateTime('2017-05-09')
$date->modify('+ 10 days')
$date->modify('-1 month')
$date->modify('next year')
$date->modify('previous month')
and so on
I am not much aware about Carbon dates in laravel. But in PHP, you can try this:
$startDate = '2017-05-09';
$days = 3;
$dayStr = $days == 1 ? 'day' : 'days';
echo date('Y-m-d', strtotime('+ '.$days. ' '.$dayStr, strtotime($startDate )));
Demo
There a number of questions regarding this, but I couldn't find the exact answer.
$datetime1 = new DateTime('2015-01-15');
$datetime2 = new DateTime(date('Y-m-d'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%m months');
As today is 2015-05-11 this returns 3 months. I would like it to always assume it's the 1st day of the month for $datetime1 so it should actually return 4 months
I supposed I could use str_replace() or some other string function to lop off the day part of $datetime1 but I'm assuming there is a more elegant method?
Thanks
If '2015-01-15' is variable. You could do:
$date = '2015-01-15';
$datetime1 = new DateTime( date('Y-m-01', strtotime($date)) );
Or use a substring function.
$date = '2015-01-15';
$datetime1 = new DateTime( substr($date, 0, 7).'-01' );
Substring would be faster but strtotime() can handle various formats.
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 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');
I have a date in this format 030512 (ddmmyy).
But I'm having trouble with converting this to a date usable format I can add days to.
Basically.. I extracted the date above from a text file, Now I need to be able to add a number of days to it. But I am having trouble parsing the date in this format.
Is there another way of doing this rather then something like this:
// I have a date in this format
$date = '030512'; // 03 May 2012
$day = substr($date,0,2);
$month = substr($date, 2,2);
$year = substr($date, 4,2);
$date_after = $day . "-" . $month . "-".$year;
// Now i need to add x days to this
$total_no_nights = 010; // must be this format
$days_to_add = ltrim($total_no_nights,"0"); // 10, remove leading zero
// how do i add 10 days to this date.
You can do this (php >= 5.3):
$date = DateTime::createFromFormat('dmy', '030512');
$date->modify('+1 day');
echo $date->format('Y-m-d');
http://www.php.net/manual/en/datetime.createfromformat.php
For php < 5.3 :
$dateArray = str_split('030512', 2);
$dateArray[2] += 2000;
echo date("d/m/Y", strtotime('+1 day', strtotime(implode('-', array_reverse($dateArray)))));
try this using the month/day/year you already have:
$date = "$month/$day/$year";
$change = '+10 day';
echo date("d/m/Y", strtotime($change, strtotime($date)));
Assuming the date will always be in the future (or at least after 1st Jan 2000), you're not far wrong:
// I have a date in this format
$date = '030512'; // 03 May 2012
$day = substr($date,0,2);
$month = substr($date, 2,2);
$year = substr($date, 4,2);
// dd-mm-yy is not a universal format but we can use mktime which also gives us a timestamp to use for manipulation
$date_after = mktime( 0, 0, 0, $month, $day, $year );
// Now i need to add x days to this
$total_no_nights = "010"; // must be this format
$days_to_add = intval( $total_no_nights ); // No need to use ltrim
// Here's the "magic". Again it returns a timestamp
$new_date = strtotime( "+$days_to_add days", $date_after );
Using the DateTime object would be easier but you say you're not on PHP5.3.
You can't do date manipulation with strings becase, well, they are not dates. In PHP, you can use Unix timestamps (which are actually integers...) or DateTime objects. In you case:
$timestamp = strtotime('-10 days', mktime(0, 0, 0, $month, $day, $year));
echo date('r', $timestamp);
... or:
$object = new DateTime("$year-$month-$day");
$object->modify('-10 days');
echo $object->format('r');
Also, please note that 010 is an octal number that corresponds to 8 in decimal.
using the convert function in sql the date can be obtained in appropriate format.
anter that operations can be performed in php to manipulate the date. i hope that answers your query.