I have a date object like so:
2016-06-30 00:00:00
What I need is take the current time, let's say
01.07.2016 14:56
Add to the first date object so it would look like this:
2016-06-30 14:56:00
I understand that one can add time by adding to a strtotime object and one can also use the add function of datetime. But what I have a hard time is figuring out how to extract the time part and then add it to the first datetime object.
Is there a simple way for it or am I going to have to kind of hack it?
Thank you
This explains how you can do it: Set Time
Just call:
date_time_set($date, 14, 56, 00); // $date is your date object
Since the date object is always midnight the value of the time is zero and can therefor be overwritten without loosing any information. If you can have more then 24 Hours to add there are other better ways. But for this we would need more information.
<?php
$d1 = '01.07.2016 14:56';
$dt = new DateTime($d1);
$date = $dt->format('m/d/Y');
$time = $dt->format('H:i:s');
$d2 ='2016-06-30 00:00:00';
$dt2 = new DateTime($d2);
$date2 = $dt2->format('Y-m-d');
$final_date=$date2.' '.$time;
echo $final_date ;
Result== 2016-06-30 14:56:00
$_SERVER['REQUEST_TIME'], if you don't want to create a new object
get the "datediff()" and then use modify to add that to existing object.
When you change the time at almost midnight and potentially you should change the date also. In this scenario if you use "modify()" and add TimeInterval to it. system will take care of the Date change also.
if you are not worried about date changes. then setTime() is always there for us.
Related
How can I add a number of days to a DateTime object without modifying the original. Every question on StackOverflow appears to be about date and not DateTime, and the ones that do mention DateTime talk about modifying the original.
Eg.
$date = new DateTime('2014-12-31');
$date->modify('+1 day');
But how can you calculate a date several days in advance without modifying the original, so you can write something like:
if($dateTimeNow > ($startDate + $daysOpen days) {
//
}
I could always just create another DateTime object, but I'd rather do it the above way.
Use DateTimeImmutable, it's the same as DateTime except it never modifies itself but returns a new object instead.
http://php.net/manual/en/class.datetimeimmutable.php
you can take the original variable in a separate variable and add no. of days in other variable so you have both(original and updated)value in different variable.
$startDate = new DateTime('2014-12-31');
$endDate = clone $startDate;
$endDate->modify('+'.$days.'days');
echo $endDate->format('Y-m-d H:i:s');
You can always use clone, too:
$datetime = clone $datetime_original;
Currently when I want to get today's date irrespective of time, I have to do the following.
$Date = new DateTime('now');
$Date->setTime(0,0,0);
Is there a more eloquent way of doing this? Preferably a one-liner
Assuming you are using PHP 5.4+
$Date = (new DateTime('now'))->setTime(0,0,0);
Or
$Date = new DateTime('midnight');
can anyone help me in a problem that i am stuck in for long hours, i am trying to add time like in format h:ia with hour and minute in format 01:00(one hour), 00:30(30 minutes), 02:00(two hour) etc.. in php.
As i have hours in format like('00:30','01:00','01:30','02:00')to the the time i already have.
i tried this
$interval="01:00";
$now=new DateTime('h:ia');
$time=($now,strtotime(+$interval,$now));
but its not working.
Any help will be appreciated.
you should check strtottime() documentation to add 1 hour simply use
strtotime("+1 hour");
you can also use time() + 60*60
There is also method in DateTime object called "add" that you can use.
From manual:
DateTime::add -- date_add — Adds an amount of days, months, years, hours, minutes and seconds to a DateTime object
You can create DateInterval object and add the interval to exsiting DateTime object.
To format date you can use format() method on DateTime object.
Edited:
Simple explode may solve your problem as well.
$interval="01:00";
$xp = explode(":", $interval);
$time=time() + (int)$xp[0] * 60*60 + (int)$xp[1]*60;
echo date("h:ia", $time);
Or more object oriented soution
$interval="01:00";
$now=new DateTime();
$now->add(new DateInterval("P0000-00-00T$interval:00"));
echo $now->format('h:ia');
Working demo
You could try something like that :
$interval = "01:00";
$yourInterval = new DateInterval('P0000-00-00T'.$interval.':00');
//Setting the date and seconds to "0"
$time = new DateTime('now');
//Or any other method to get your DateTime
$time->add($yourInterval);
I think that in all cases, you should use DateTime and DateInterval, OR strtotime()
I'm not sure about mixing them.
I prefer to use DateTime and DateInterval because it's more Object oriented.
In all case i advice not to explode for date operation. It's highly dangerous because of many many problems you can't predict (Like bisexstil seconds etc...)
EDIT :
I guess that if you're just manipulating time, and not date, exploding your string is not a problem, the mainly problem is if you use dates.
Okay so I have an array of results from a table, we have a start and end time we are retrieving. the start/end time would look something like this:
1345497551
Now I'm trying to convert this to a real time, for instance 1345497551 might become 2012/05/09 17:23:12 or something. I've found a few things but none seem to work correctly. one solution I tried, according to what someone was saying on another question on here, was
$createdate = date('H:i:s',$numberofsecs);
where $numberofsecs was the time pulled in from the array. but this only ever outputs 17:00:00 repeatedly for every time we had available for testing.
How can I go about making this work correctly?
Assuming that that's a standard unix timestamp string (seconds since midnight 1/1/1970), then you should be able to use date as you mentioned, but just modify the format string:
echo date('Y/m/d H:i:s', $numberofsecs);
The example you mention where you were always getting 17:00:00 could have been because your test cases were all only datestamps, encoded as timestamps, and having an offset from GMT . . .
I have tried below code:
$ts = 1345497551;
$date = new DateTime("#$ts");
echo $date->format('U = Y-m-d H:i:s');
output : 1345497551 = 2012-08-20 21:19:11
I have a date stored in a database in this format:
2011-02-23 13:00:00
I need to return it in ISO8601 format, but it needs to be set to a specific time zone (which is not necessarily the time zone of the server.) What I want to return is this:
2011-02-23T13:00:00-0600
Using this code:
echo date(DATE_ISO8601, strtotime("2011-02-23 13:00:00"));
I get this:
2011-02-23T13:00:00+0000
Is there any way to reset the time zone in the date or strtotime function, or do I need to strip off the 5 rightmost characters and concatenate the desired timezone stamp to the remaining date/time?
EDITED TO ADD:
Although I did accept the solution below of using new DateTime and setting new DateTimeZone, I found an easier way if you don't need to keep resetting the time zone:
date_default_timezone_set('America/Chicago');
$startTime = date(DATE_ISO8601, strtotime("2011-02-23 13:00:00"));
You could use the DateTime class. Datetime objects can be initialized with a specific time zone, and easily transposed to others.
Modified from the manual:
$date = new DateTime('2011-02-23 13:00:00', new DateTimeZone('Pacific/Nauru'));
echo $date->format('c') . "\n";
$date->setTimezone( new DateTimeZone('Europe/Berlin'));
echo $date->format('c') . "\n";