PHP Reduce days, hours and minutes from a datetime - php

Need to reduce no of days, hours and minutes from a datetime using php.
Datetime is of the format Y-m-d H:i:s
eg: Suppose datetime is 2013-03-20 14:20:00. How to reduce 2 days , 3 hours and 10 minutes from this such that it results in 2013-03-18 11:10:00.

<?php
$date = new DateTime("2013-03-20 14:20:00");
$dateIncremented = $date->sub(date_interval_create_from_date_string('2 days 3 hours 10 minutes'));
$finalDate = $date->format("Y-m-d H:i:s");
echo $finalDate;
?>
Allirght. An Alias. But Readable format

use the DateTime object :
$date = new DateTime('2013-03-20 14:20:00');
$date->sub(new DateInterval('P2DT3H10M'));
echo $date->format('Y-m-d H:i:s');

You must explore DateTime::sub and DateInterval, and about DateInterval format
echo \DateTime::createFromFormat('Y-m-d H:i:s', '2013-03-20 14:20:00')
->sub(new \DateInterval('P2DT3H10M'))
->format('Y-m-d H:i:s'); // 2013-03-18 11:10:00

Related

PHP zero out the time for a date and add 3 days

I'm looking for an elegant/efficient way to take out the time portion of a datetime in format 'Y-m-d H:i:s', and then add 3 days.
Currently the solution is:
date('Y-m-d 00:00:00', strtotime("+3 days", strtotime('2017-01-23 05:32:12')));
where 2017-01-23 05:32:12 is the date, and this correctly outputs 2017-01-26 00:00:00.
It just feels like there has to be a better way to do this.
Thanks
DateTime() offers several of ways to do this. None of them are any less verbose than your current method:
// Plain old DateTime()
$date = (new DateTime('2017-01-23 05:32:12'))->modify('+3 days')->format('Y-m-d 00:00:00');
// DateTme using DateInterval to add three days
$date = (new DateTime('2017-01-23 05:32:12'))->add(new DateInterval('P3D'))->format('Y-m-d 00:00:00');
// DateTime setting the date to midnight instead of using 00:00:00
$date = (new DateTime('2017-01-23 05:32:12'))->modify('+3 days')->modify('midnight')->format('Y-m-d H:i:s');
If the date is today you can shorten this a bit:
$date = (new DateTime('+3 days'))->format('Y-m-d 00:00:00');
you can use DateTime class
$date = new DateTime('2017-01-23 05:32:12');
$date->modify('+3 days');
$outputDateString = $date->format('Y-m-d 00:00:00');
one line:
$date = ( new DateTime('2017-01-23 05:32:12'))->modify('+3 days')->format('Y-m-d 00:00:00');

date_create function is not working in php

I have an issue.I need to calculate the time difference but here the date_create function is not showing any output in php.I am explaining my code below.
while($row=mysqli_fetch_assoc($orderqry)){
$exit_date=$row['delivered_datetime'];
if($exit_date!=='0000-00-00 00:00:00'){
$deldate=date_create($exit_date);
$today=date_create(date('Y-m-d H:i:s'));
echo $today;exit;
$interval = date_diff($deldate, $today);
$hours = $interval->format('%h');
}
}
Here i am trying to echo $today but its not giving any result.Here i need to calculate the time difference in hour.In my DB i have existing time in the format of date('Y-m-d H:i:s').Please help me.
$today is DateTime instance. To echo it you need to call its format() method.
echo $today->format('Y-m-d H:i:s');
date_create is an alias of: DateTime::__construct(). see below example, I have assigned 6 Hour ago date in $exit_date. so ideally difference from current date should be 6 Hour.
$exit_date = date('Y-m-d H:i:s', strtotime('-6 Hours'));
$deldate=date_create($exit_date); //returns datetime class object
$today=date_create(date('Y-m-d H:i:s'));
$interval = date_diff($deldate, $today);
echo $hours = $interval->format('%h'); // output 6 hours
If you want to print date from $today variable then use:
echo $today->format('Y-m-d H:i:s'); //output: today's date in Y-m-d H:i:s format
for more detail have a look at http://php.net/manual/en/book.datetime.php

Date format PHP

I am trying to format a date as 2015-07-12 15:00 from the values declared in my variables
// unix
$date = 1436713200
// string
$time = '15:00';
to get a date format 2015-07-12 15:00 but failing, using this
$newdate = date('Y-m-d H:i:s', $date.' '.$time);
I get 'A non well formed numeric value encountered'. Can anyone help? I understand it is possibly due to the mix of string and unix but unsure how to get round this.
I would suggest you to use DateTime instance to avoid timezone issues:
$d = date_create('#1436713200'); // creates DateTime instance
$d->setTime(15, 00); // sets current time to desired hours, minutes
echo $d->format('Y-m-d H:i:s'); // prints it out with format specified
//⇒ 2015-07-12 15:00:00
You do not have to provie the $time variable. Unix time is a full date with time.
Use:
$newdate = date('Y-m-d H:i:s', $date);
Use this
$date = date('Y-m-d','1436713200');
// string
$time = '15:00';
echo $newdate = date('Y-m-d H:i', strtotime($date.' '.$time));

PHP, need to subtract 12 hours and 30 minutes from a DateTime

I have a PHP DateTime variable.
How can I reduce or subtract 12hours and 30 minutes from this date in at PHP runtime?
Subtract 12 Hours and 30 minutes from a DateTime in PHP:
$date = new DateTime();
$tosub = new DateInterval('PT12H30M');
$date->sub($tosub);
The P stands for Period. The T stands for Timespan.
See DateTime, DateTime::sub, and DateInterval in the PHP manual. You'll have to set the DateTime to the appropriate date and time, of course.
Try with:
$date = new DateTime('Sat, 30 Apr 2011 05:00:00 -0400');
echo $date->format('Y-m-d H:i:s') . "\n";
$date->sub(new DateInterval('PT12H30M'));
echo $date->format('Y-m-d H:i:s') . "\n";
//Result
2011-04-30 05:00:00
2011-04-29 16:30:00
Try strtotime() function:
$source_timestamp=strtotime("Sat, 30 Apr 2011 05:00:00 -0400");
$new_timestamp=strtotime("-12 hour 30 minute", $source_timestamp);
print date('r', $new_timestamp);
Maybe it will be useful for some cases
$date = new DateTime();
$date->modify('-12 hours -30 minutes');
echo $date->format('H:i:s');
try using this instead
//set timezone
date_default_timezone_set('GMT');
//set an date and time to work with
$start = '2014-06-01 14:00:00';
//display the converted time
echo date('Y-m-d H:i',strtotime('+1 hour +20 minutes',strtotime($start)));
If you are not so familiar with the spec of DateInterval like PT12H30M you can proceed with more human readable way using DateInterval::createFromDateString as follows :
$date = new DateTime();
$interval = DateInterval::createFromDateString('12 hour 30 minute');
$date->sub($interval);
Or with direct interval in sub function like below :
$date = new DateTime();
$date->sub(DateInterval::createFromDateString('12 hour 30 minute'));
Store it in a DateTime object and then use the DateTime::sub method to subtract the timespan.
I used in one line, for 12 hours only, and just as an hour display
$date = new DateTime(); $date->modify('-12 hours'); echo $date->format('H')-0;
I used the -0 since sometimes it put a 0 in front of the digit unless I done that, strange.
Here is detailed description of date function,
Using simply strtotime
echo date("Y-m-d H:i:s",strtotime("-12 hour -30 minutes"));
Using DateTime class
$date = new DateTime("-12 hour -30 minutes");
echo $date->format("Y-m-d H:i:s");

adding 1 day to a DATETIME format value

In certain situations I want to add 1 day to the value of my DATETIME formatted variable:
$start_date = date('Y-m-d H:i:s', strtotime("{$_GET['start_hours']}:{$_GET['start_minutes']} {$_GET['start_ampm']}"));
What is the best way to do this?
There's more then one way to do this with DateTime which was introduced in PHP 5.2. Unlike using strtotime() this will account for daylight savings time and leap year.
$datetime = new DateTime('2013-01-29');
$datetime->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
// Available in PHP 5.3
$datetime = new DateTime('2013-01-29');
$datetime->add(new DateInterval('P1D'));
echo $datetime->format('Y-m-d H:i:s');
// Available in PHP 5.4
echo (new DateTime('2013-01-29'))->add(new DateInterval('P1D'))->format('Y-m-d H:i:s');
// Available in PHP 5.5
$start = new DateTimeImmutable('2013-01-29');
$datetime = $start->modify('+1 day');
echo $datetime->format('Y-m-d H:i:s');
If you want to do this in PHP:
// replace time() with the time stamp you want to add one day to
$startDate = time();
date('Y-m-d H:i:s', strtotime('+1 day', $startDate));
If you want to add the date in MySQL:
-- replace CURRENT_DATE with the date you want to add one day to
SELECT DATE_ADD(CURRENT_DATE, INTERVAL 1 DAY);
The DateTime constructor takes a parameter string time. $time can be different things, it has to respect the datetime format.
There are some valid values as examples :
'now' (the default value)
2017-10-19
2017-10-19 11:59:59
2017-10-19 +1day
So, in your case you can use the following.
$dt = new \DateTime('now +1 day'); //Tomorrow
$dt = new \DateTime('2016-01-01 +1 day'); //2016-01-02
Use strtotime to convert the string to a time stamp
Add a day to it (eg: by adding 86400 seconds (24 * 60 * 60))
eg:
$time = strtotime($myInput);
$newTime = $time + 86400;
If it's only adding 1 day, then using strtotime again is probably overkill.
You can use
$now = new DateTime();
$date = $now->modify('+1 day')->format('Y-m-d H:i:s');
You can use as following.
$start_date = date('Y-m-d H:i:s');
$end_date = date("Y-m-d 23:59:59", strtotime('+3 days', strtotime($start_date)));
You can also set days as constant and use like below.
if (!defined('ADD_DAYS')) define('ADD_DAYS','+3 days');
$end_date = date("Y-m-d 23:59:59", strtotime(ADD_DAYS, strtotime($start_date)));
I suggest start using Zend_Date classes from Zend Framework. I know, its a bit offtopic, but I'll like this way :-)
$date = new Zend_Date();
$date->add('24:00:00', Zend_Date::TIMES);
print $date->get();
Using server request time to Add days. Working as expected.
25/08/19 => 27/09/19
$timestamp = $_SERVER['REQUEST_TIME'];
$dateNow = date('d/m/y', $timestamp);
$newDate = date('d/m/y', strtotime('+2 day', $timestamp));
Here '+2 days' to add any number of days.
One liner !
echo (new \DateTime('2016-01-01 +1 day'))->format('Y-m-d H:i:s');

Categories