Adding minutes to date formatting syntax - php

I am having trouble adding a time and time interval to a date.
I use a javascript datepicker that gives date in the form: 05/06/2013
I can convert this into a date time format with
$eventdate = date("Y-m-d H:i:s",strtotime($eventdate));
This gives: 2013-05-06 00:00:00
I get the time from a separate variable in minutes. Eight o'clock would be 480.
However, I cannot seem to find syntax to add 480 minutes to the date.
Have tried, for example,
echo date("Y/m/d h:i:s", strtotime("+480 minutes", $eventdate));
but this gives me the default 1969 date.
Thanks for any help or suggestions!

Use the DateTime class, it's easier to work with:
$eventdate = \DateTime::createFromFormat('Y/m/d h:i:s', $eventdate);
$eventdate->modify('+480 minutes');
echo $eventdate->format('Y/m/d h:i:s');

Related

Add variable string to datetime eg. 2 weeks

is it possible to add a variable string like '2 day, 2 weeks or even 4 hours' to a date time in PHP.
For example:
I have a date time like this: '2017-08-02 12:00'
now the user choose an interval like '4 hours or 2 weeks'
now the user choice should be added to the date time.
Is this possible?
I don't want the whole code, maybe just an advice how to do that.
thanks
Yes, use
$userDate = strtotime('2017-08-02 12:00:00');
echo date('Y-m-d H:i:s', strtotime('+4 hours', $userDate));
to get date after 4 hours
Example
Explanation
strtotime converts about any English textual datetime description into a Unix timestamp. Most commonly it's used with actual date string or with difference string. E.g. +5 months, 'next Monday' and so on. It will return Unix timestamp - integer that represents how much seconds there is after 1970-01-01 (1970-01-01 00:00:00 is 0, 1970-01-01 00:01:00 is 60 and so on).
So in strtotime('2017-08-02 12:00:00') we convert date to integer for later use.
strtotime('+4 hours', $userDate) - here we use our date as "now" parameter (by default it's time()) and requesting to return timestamp after 4 hours.
echo date('Y-m-d H:i:s', ...); - date accepts format and Unix timestamp to convert from integer back to human readable text.
May be you are looking for this:
http://php.net/manual/en/datetime.modify.php
$date = new DateTime('2006-12-12');
$date->modify('+1 day');
echo $date->format('Y-m-d');
For a datetime you can use the add method but you have to put in the correct code for the amount to add.
$my_date = new DateTime();
$addition = 4;
$my_new_date = $my_date->add(new DateInterval("P${addition}D"));
echo $my_new_date->format('Y-m-d H:i:s');
Where addition is your variable that you want to add.

Displaying a date in php 24 hours later, issues

I'm basically trying to add 24 hours to a date with php and display it but it keeps adding only 23 hours in stead of 24 hours.
<?php
$create_time = strtotime('2015-03-18 20:03:23');
$set_time = $create_time + 3600*24;
echo gmdate("Y-m-d H:i:s", $set_time);
?>
So the result that I'm getting out of this is:
2015-03-19 19:03:23
but it's this what should be coming out of this:
2015-03-19 20:03:23
I'm new at working with these time functions and I can't figure out why it keeps getting adding 23 hours. Obviously I can multiply it by 25 and get 24 hours but that doesn't make sense to me.
So my question is: what's the proper way to add 24 hours to a date?
I would do it like so:
date("Y-m-d H:i:s", strtotime("+1 day"));
strtotime() uses default time zone, gmdate() uses Greenwich Mean Time (GMT). Try using date() instead.
<?php
$create_time = strtotime('2015-03-18 20:03:23');
$set_time = $create_time + 3600*24;
echo date("Y-m-d H:i:s", $set_time);
?>

What is the proper way of converting a timestamp to a human readable date?

i've got 2 time stamps: $start_time = 1312346227; and $end_time = 1312346466;
and i am trying to substract them to see the time inbetween $end_time = $end_time - $start_time;
and i get 239.
What is the proper way of converting this to a human readable date?
if i try echo date("h:i:s A",$end_time); i get 04:03:59 and it should be 00:03:59
any ideas?
Thanks
If you have PHP 5.3, use the DateInterval class.
Example stolen from the manual page on DateInterval::format():
<?php
$january = new DateTime('2010-01-01');
$february = new DateTime('2010-02-01');
$interval = $february->diff($january);
// %a will output the total number of days.
echo $interval->format('%a total days')."\n";
// While %d will only output the number of days not already covered by the
// month.
echo $interval->format('%m month, %d days');
?>
You get addiionl four hours, because of your timezone. Remember that unix timestamp is the number of seconds since 1970-01-01 00:00:00 UTC. If you (or your server) are in UTC+4 TZ, then date() will implicitly do a timezone conversion to your local time.
Solution? Use gmdate() instead
You need to set your timezone correctly. http://www.php.net/manual/en/function.date-default-timezone-set.php

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

PHP get 31 days distance from starting date

How can I get what date it will be after 31 days starting with $startDate, where $startDate is a string of this format: YYYYMMDD.
Thank you.
strtotime will give you a Unix timestamp:
$date = '20101007';
$newDate = strtotime($date.' + 31 days');
you can then use date to format that into the same format, if that's what you need:
echo date('Ymd', $newDate);
If you're using PHP 5.3:
$date = new DateTime('20101007');
$date->add(new DateInterval('P31D'));
echo $date->format('Y-m-d');
The pre-5.3 date functions are lacking, to say the least. The DateTime stuff makes it much easier to deal with dates. http://us3.php.net/manual/en/book.datetime.php
Just a note that +1 month will also work if you want the same date on the next month and not 31 days exactly each time.
echo date('Y m d',strtotime('+31 Days'));

Categories