php string in a date format, add 12 hours - php

I have this string object in my php array
"2013-03-05 00:00:00+00"
I would like to add 12 hours to the entry within PHP, then save it back to string in the same format
I believe this involves converting the string to a date object. But I'm not sure how smart the date object is and if I need to tell it formatting parameters or if it is supposed to just take the string
$date = new DateTime("2013-03-05 00:00:00+00");
$date->add("+12 hours");
//then convert back to string or just assign it to a variable within the array node
I was getting back empty values from this method or a similar one I tried
How would you solve this issue?
Thanks, your insight is appreciated

Change add() to modify(). add() expects a DateInterval object.
<?php
$date = new DateTime("2013-03-05 00:00:00+00");
$date->modify("+12 hours");
echo $date->format("Y-m-d H:i:sO");
See it in action
Here's an example using a DateInterval object:
<?php
$date = new DateTime("2013-03-05 00:00:00+00");
$date->add(new DateInterval('PT12H'));
echo $date->format("Y-m-d H:i:sO");
See it in action

Change this line
$date->add("+12 hours");
with
$date->add(new DateInterval("PT12H"));
this will add 12 hours to your date
Look at the DateInterval constructor page to know how to build the DateInterval string

Use this to add hours,
$date1= "2014-07-03 11:00:00";
$new_date= date("Y-m-d H:i:s", strtotime($date1 . " +3 hours"));
echo $new_date;

If you have dynamic interval, this way will avoid errors of wrong format for $dateDiff:
$dateDiff = "12 hours";
$interval = DateInterval::createFromDateString($dateDiff);
$date = new DateTime("2013-03-05 00:00:00+00");
$date->add($interval);
echo $date->format("Y-m-d H:i:sO");

Related

PHP: why year not subtracted from date?

The below code doesn't subtract 1 year from the date. Why?
$date1 = '2021-06-02';
$date2 = new \DateTime(date($date1, strtotime('-1 year')));
echo $date2->format('Y-m-d'); // outputs the same date 2021-06-02
Part of your problem is that the date function's first argument is the format of the date.
https://www.php.net/manual/en/function.date.php
So what is happening is that you are creating a date string with the format of '2021-06-02'.
https://www.php.net/manual/en/datetime.format.php
This doesn't use anything from the timestamp that you are providing so this string is passed to the constructor of DateTime and creating the date instead of the one from the year previous.
Please use this code. Its always works for me.
$date1 = '2021-06-02';
$date2 = date("Y-m-d", strtotime("-1 year", strtotime($date1)));
echo $date2; //Output 2020-06-02
This is for Date time object:
$dt = new DateTime('2021-06-02');
$minusOneYearDT = $dt->sub(new DateInterval('P1Y'));
$minusOneYear = $minusOneYearDT->format('Y-m-d');
echo $minusOneYear;
OR make a small solution:
$time = new DateTime('2021-06-02');
$newtime = $time->modify('-1 year')->format('Y-m-d');
echo $newtime;
Your code is a bit of a muddle:
date() takes as parameters a format string, and an integer representing a point in time; it then applies the format to create a string for that date and time
strtotime() takes a string, interprets it as a point in time, and returns an integer timestamp
new DateTime() takes a string, in any of the formats strtotime would accept, but creates an object representation rather than returning an integer
You've tried to use all of them at once, and got in a mess:
Your call to date() has a first parameter of '2021-06-02', which isn't a valid format.
Your call to strtotime() has a parameter of '-1 year', which will just be interpreted as "1 year before now", not relative to anything else you've specified.
Using both of those functions and then passing to new \DateTime() doesn't make a lot of sense, since the object can do all the same things those functions can do.
If you want to use the integer-based functions, you could write this:
$date1 = '2021-06-02';
$date2 = strtotime("$date1 -1 year");
echo date('Y-m-d', $date2);
If you want to use the object-based functions, you could write this:
$date1 = '2021-06-02';
$date2 = new \DateTime("$date1 -1 year");
echo $date2->format('Y-m-d');
Or this (note the use of DateTimeImmutable instead of DateTime to avoid the modify method changing the $date1 object:
$date1 = new \DateTimeImmutable('2021-06-02');
$date2 = $date1->modify('-1 year');
echo $date2->format('Y-m-d');

adding one day to a custom formatted date string

I have a string in the format YYYYMMDDHH24MISS that is year, month, day, hours, minutes, seconds. I want to convert this to a date, add one day to it and return it in the same format. Sounds simple but I am unable to get this to work. I have tried a number of different ways where $field3 contains the date string for example:
$end_date = strtotime(substr($field3,1,8));
$date_interval = DateInterval::createFromDateString('1 day');
$new_end_date = date_add($end_date, $date_interval);
$field3 = ($new_end_date->format('YYYYMMDD')).substr($field3,8,6);
In this example $new_end_date contains "false".
Example date time string: 20170912124159 being 12/09/2017 12:41:59
The format of your input string can be parsed by the constructor of class DateTime (and date_create() and strtotime()) without problems.
$date = new DateTime('20170912124159');
$date->add(new DateInterval('P1D'));
echo($date->format('Y-m-d H:i:s'));
# The output is:
# 2017-09-13 12:41:59
You can, as well, format the date as string using the format YmdHis to get the modified date in the same format as the input string.
echo($date->format('YmdHis'));
# 20170913124159
Read about DateTime and DateInterval.
You can try something like this:
$date = DateTime::createFromFormat('YmdHis', '20170912131313');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d');
For more information: http://php.net/manual/en/datetime.add.php
Please try this
$date = date("Y/m/d H:i:s"); // or '2017/09/30 20:24:00'
$ndate = date('Y/m/d H:i:s', strtotime($date . ' +1 day'));
echo 'date after adding 1 day: ' . $ndate;

PHP date and time from an xml import

I have an xml file where the date stored in this format:
<sun rise="2014-05-30T02:51:30" set="2014-05-30T18:31:22"/>
My php process this value to a variable named $sunrise
2014-05-30T02:51:30
But my timezone is +2 so I have to add 2 more hours.
The only problem with this its just a string.
I dont have any idea to how to convert it to date.
Since you already got the time inside that element (2014-05-30T02:51:30) you could just use strtotime() or alternatively, you could also use DateTime + DateInterval to add 2 more hours. Consider this example:
$sunrise = '2014-05-30T02:51:30';
$date = new DateTime($sunrise);
$date->add(new DateInterval('PT2H'));
echo $date->format('Y-m-d H:i:s');
// outputs: 2014-05-30 04:51:30
echo date('Y-m-d H:i:s', strtotime($sunrise . ' +2 hours'));
// outputs: 2014-05-30 04:51:30

Quicker way to get date of ISO week from a date string?

I'm being passed a date string (most likely in ISO8601 format) and need to convert it to the date of the ISO week to store as a DATETIME column in MySQL. To initialize the DateTime object the I want to save, I'm doing the following:
$date = new DateTime("now");
$date = new DateTime( $date->format("o-\WW") );
echo $date->format(DateTime::ISO8601) . "\n";
Since I'm using Doctrine2, I need to pass my entity a DateTime object. Is there a way to avoid making 2 DateTime objects to get the same result? Should I drop back to the date function and use that as the argument to the DateTime constructor?
$date = new DateTime( date("o-\WW", strtotime("now") );
You could use setISODate to update the first DateTime object using the week and year of the object via format():
$date = new DateTime();
$date->setISODate($date->format('o') , $date->format('W'));
echo $date->format(DateTime::ISO8601);
You could use modify() method of DateTime object.
$date = new DateTime();
$date->modify('sunday this week');
echo $date->format(DateTime::ISO8601) . "\n";
Note that if you want the first day of the week to be something other than Sunday, you will likely need to do something like the following. This example considers Monday as the first day of the week, thus for dates on a Sunday, you would need to get the date of the Monday from the previous week.
$date = new DateTime();
if ($date->format('D') === 'Sun') {
$date->modify('monday last week');
} else {
$date->modify('monday this week');
}
echo $date->format(DateTime::ISO8601) . "\n";
You can probably use date like this:
$date = new DateTime( date('o-\WW') );
Though that formatting looks a bit strange. :p You can of course also use some other method/function the class has to offer to change/modify the date.

How do I remove 3 months from a date?

Assume the date is:
$date = "2011-08-28";
It need to calculate 3 months previous to $date - how can that be done?
$new_timestamp = strtotime('-3 months', strtotime($date));
You can then use the new timestamp with the php date() function to display the new date how you wish, for example:
echo date("Y-m-d",$new_timestamp);
For me this way is much better because the code is more readable.
$datetime = Datetime::createFromFormat('Y-m-d', "2011-08-28");
$datetime->modify('-3 months');
echo $datetime->format('Y-m-d');
edit: I'm an idiot. You could do the above as follows
$datetime = new Datetime("2011-08-28");
$datetime->modify('-3 months');
echo $datetime->format('Y-m-d');
edit: As pointed out by calumbrodie, you can use the sub method instead of inverting the interval and adding it to the date object
I was trying to do something similar to the original question. I needed to subtract 1 day from a DateTime object. I know the other solutions work, but here's another way I liked better. I used this function:
function getPreviousDay($dateObject){
$interval = new DateInterval('P1D');
$dateObject->sub($interval);
return $dateObject;
}
$dateObject is a DateTime object that can have any date you wan't, but as I wanted the current date, I wrote:
$dateObject = new DateTime('now');
What my function does, is subtract 1 day from the date it receives, but you can modify it so it subtracts 3 months modifying the DateInterval constructor like this:
$interval = new DateInterval('P3M');
$dateObject->sub($interval);
return $dateObject;
You can find the explanation on the string format used in the DateInterval constructor here
DateInterval constructor documentation
There you'll se that letter 'P' (period) is mandatory, the you use an int to specify period length and then specify the period type, in the case of months 'M'. It looks as if there was an error in the documentation, it says that "M" is used for months and minutes, but it actually works for months. If you need to use minutes, you must specify "PTM", for example "PT3M" for 3 minutes, outside the table it says you must use the "T" identifier for time intervals.
edit:
To give a complete example, you have to use this format for a full date time interval:
$format = 'P1Y2M4DT2H1M40S';
This will represent an interval of 1 year, 2 months, 4 days, 2 hours,1 minute and 40 seconds
Hope this helps someone
<?php
$threemonthsago = mktime(0, 0, 0, date("m")-3, date("d"), date("Y"));
?>
<?php
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('10 days'));
echo date_format($date, 'Y-m-d');
?>

Categories