I have a date and timestamp in HL7 format : 201402181659
That represents 2014 02 18 at 16:59
it's offset by +11:00 hours so I need to be able to add 11 hours to the date/time. Any ideas?
You can use DateTime::createFromFormat() to parse the string and then DateTime::modify() to add 11 hours to it:
$date = DateTime::createFromFormat('YmdHis', '201402181659');
$date->modify('+11 hours');
echo $date->format('YmdHis');
You can also use DateTime::add() with DateInterval() to add the 11 hours:
$date = DateTime::createFromFormat('YmdHis', '201402181659');
$date->add(new DateInterval('PT11H'));
echo $date->format('YmdHis');
Related
how can i convert a time stamp to Monday corresponding with that week?
example:
1473750000 (Tue, 13 Sep 2016 07:00:00 GMT)
// coding magic
1473638400 (Mon, 12 Sep 2016 00:00:00 GMT )
To convert timestamps, you can use a PHP Function gmdate(). I already use this on some of my projects/sites.
$timestamp = strtotime("+1 hour"); //Uk Time
echo gmdate("H:i", $timestamp); //Hour:Minutes
For the other parts of gmdate(), check the PHP Manual.
I also add my solution to the problem, using DateInterval.
function getLastMonday($timestamp){
// Initialize DateTime object
$date = new DateTime();
$date->setTimestamp($timestamp);
// Calculate num of days to substract and create date interval
$dayOfWeek = $date->format("w");
$interval = new DateInterval("P".(($dayOfWeek+6)%7).'D');
$interval->invert = 1;
// Substract the Date interval
$date->add($interval);
return $date;
}
I've found an answer using the documentation provided in the answers:
$date = date('Y-m-d',strtotime('this monday this week',1473750000));
This question already has answers here:
Add number of days to a date
(20 answers)
Closed 6 years ago.
Hello all i am trying to add 30 days to my date. I am using below coding.
<?php
$next_due_date = date('05/06/2016', strtotime("+30 days"));
echo $next_due_date;
?>
But it is returning to "05/06/2016" only!
Please help me!
Do not use php's date() function, it's not as accurate as the below solution and furthermore it is unreliable in the future.
Use the DateTime class
<?php
$date = new DateTime('2016-06-06'); // Y-m-d
$date->add(new DateInterval('P30D'));
echo $date->format('Y-m-d') . "\n";
?>
The reason you should avoid anything to do with UNIX timestamps (time(), date(), strtotime() etc) is that they will inevitably break in the year 2038 due to integer limitations.
The maximum value of an integer is 2147483647 which converts to Tuesday, 19 January 2038 03:14:07 so come this time; this minute; this second; everything breaks
Source
Another example of why I stick to using DateTime is that it's actually able to calculate months correctly regardless of what the current date is:
$now = strtotime('31 December 2019');
for ($i = 1; $i <= 6; $i++) {
echo date('d M y', strtotime('-' . $i .' month', $now)) . PHP_EOL;
}
You'd get the following sequence of dates:
31 December
31 November
31 October
31 September
31 August
31 July
31 June
PHP conveniently recognises that three of these dates are illegal and converts them into its best guess, leaving you with:
01 Dec 19
31 Oct 19
01 Oct 19
31 Aug 19
31 Jul 19
01 Jul 19
Please try this.
echo date('m/d/Y',strtotime('+30 days',strtotime('05/06/2016'))) . PHP_EOL;
This will return 06/06/2016. Am assuming your initial date was in m/d/Y format. If not, fret not and use this.
echo date('d/m/Y',strtotime('+30 days',strtotime(str_replace('/', '-', '05/06/2016')))) . PHP_EOL;
This will give you the date in d/m/Y format while also assuming your initial date was in d/m/Y format. Returns 05/07/2016
If the input date is going to be in mysql, you can perform this function on mysql directly, like this.
DATE_ADD(due_date, INTERVAL 1 MONTH);
The first parameter is the format, not the current date.
<?php
$next_due_date = date('d/m/Y', strtotime("+30 days"));
echo $next_due_date;
Demo: https://eval.in/583697
If you want to add the 30 days to a particular starting date use the second parameter of the strtotime function to tell it where to start.
When in doubt about how a function works refer to the manual.
http://php.net/manual/en/function.strtotime.phphttp://php.net/manual/en/function.date.php
You need to provide date format like d/m/Y instead of 05/06/2016
Try
$old_date = '05-06-2016';
$next_due_date = date('d-m-Y', strtotime($old_date. ' +30 days'));
echo $next_due_date;
$date = "1998-08-14";
$newdate = strtotime ( '30 day' , strtotime ( $date ) ) ;
$newdate = date ( 'Y-m-j' , $newdate );
echo $newdate;
Found the above code
I have a date, and need to add 24 Months (and not 2 Year) to it. This is what I tried:
strtotime("+24 months", $mydate);
If my date is, 20th Dec 2013 then the computed date is coming as 10th Dec 2015, whereas my expected date was 20th Dec 2015.
I know, what is going behind the scene:
2 Year: 365 days x 2 = 730 days
24 Months: 24 x 30days = 720 days
This gives me the missing 10 days. But how to over come this issue.
In Java we have Calendar class, which takes care of such calculations. However, I din't find anything here.
Can this issue be resolved.? Or I need to handle it manually?
You should always use the DateTime() class for anything like that.
i.e
$date = new DateTime("UTC");
//get date in 24months time:
$date->add(new DateInterval("P24M"));
//output date:
echo $date->format("d/m/Y H:i:s");
By using the DateTime and DateInterval classes, you can be sure that it will account of leap years and other such irregularities in dates.
See more at: http://php.net/manual/en/class.datetime.php
I hope this helps.
$mydate = "2014-10-01";
echo date('d-m-Y',strtotime("+24 months", strtotime($mydate)));
DateTime should work perfectly here:
$date = new DateTime("20th Dec 2013");
echo $date->format("d-m-Y");
$date->add(new DateInterval('P24M'));
echo $date->format("d-m-Y");
Output:
20-12-2013
20-12-2015
Demo
Sidenote:
I couldn't reproduce your error with:
echo date("d-m-Y", strtotime("+24 months", $mydate = strtotime("2013-12-20")));
See:
http://3v4l.org/HURAt
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");
I need to find a date after a certain number of weeks. For example, if the start date is Saturday, 31 October 2009, and if I choose 16 weeks then I need to find the date after sixteen Saturdays.
Thanks in advance.
You can use strtotime:
// Oct 31 2009 plus 16 weeks
echo date('Y-m-d', strtotime('2009-10-31 +16 week')); // outputs 2010-02-20
// next week
echo date('Y-m-d', strtotime('+1 week')); // outputs 2009-11-07
strtotime() is what you want: it takes an expression and turns it into a date object. Also see date():
$date = '31 october 2009';
$modification = '+ 16 weeks';
echo date('Y-m-d (l)', strtotime("$date")); //2009-10-31 (Saturday)
echo date('Y-m-d (l)', strtotime("$date $modification")); // 2010-02-20 (Saturday)
If you're using PHP 5.2.0 or later, you can use the DateTime class
<?php
$date = new DateTime('31 October 2009'); // This accepts any format that strtotime() does.
// Now add 16 weeks
$date->modify('+16 weeks');
// Now you can output it however you wish
echo $date->format('Y-m-d');
?>
Why not just use unix time, subtract 7 * 24 * 3600 * 16 from that and then convert that back into the date that you need.
This will help with the conversion back:
http://php.net/manual/en/function.date.php