1

I have a php time "09:00" that I want to set as the time for a date.

$date="2016-08-21 00:00:00.000000";
$time="09:00";

So ideally:

$datetime=$date+$time;//<----------what is the function for this
echo $datetime;

gives "2016-08-21 09:00:00.000000"

or

$date="2016-08-21 00:00:00.000000";
$time="17:30";

then

$datetime=$date+$time;//<----------what is the function for this
echo $datetime;

gives "2016-08-21 17:30:00.000000". A seemingly simple requirement I cannot find a solution to.

Datadimension
  • 872
  • 1
  • 12
  • 31
  • [hint](http://www.php.net/manual/en/datetime.settime.php) – Mark Baker Aug 21 '16 at 22:51
  • Marks hint was the function date_time_set which takes integers - I have a php time object eg "09:00" do I really need to convert to string and parse it ??? – Datadimension Aug 21 '16 at 22:55
  • 1
    You do realise that you can easily split `09:00` into an array of integers by exploding on `:`? – Mark Baker Aug 21 '16 at 23:05
  • 1
    `$date="2016-08-21 00:00:00.000000"; $time="09:00"; $dto = new DateTime($date); list($hours, $minutes) = explode(':', $time); $dto->setTime($hours, $minutes);` [Demo](https://3v4l.org/BG5EC) – Mark Baker Aug 21 '16 at 23:07
  • Mark - this was indeed the only simplest way I can see - add it as an answer – Datadimension Aug 21 '16 at 23:19

2 Answers2

1

try this :

<?php
  $date = new DateTime('2000-01-01');
  $date->add(new DateInterval('PT10H30S'));
  echo $date->format('Y-m-d H:i:s') . "\n";
 ?>
Hamonbatra
  • 178
  • 10
  • So I need to parse the php time string for "09:00" - I hoped to avoid this as not efficient if there was a datetime function instead of string parsing – Datadimension Aug 21 '16 at 22:57
  • No thats complete creation - I have the date already, I want to set the time part – Datadimension Aug 21 '16 at 23:04
  • there is no showing what to do with $time as a variable hence not perfect my friend – Datadimension Aug 21 '16 at 23:10
  • http://stackoverflow.com/questions/21742329/how-to-create-a-dateinterval-from-a-time-string. This approach is better, just need to create DateInterval string by this pattern. Upvote by me. – jaro1989 Aug 21 '16 at 23:22
  • Not a solution, I stated 2 variables not a construct, hence now downvoting and setting my solution as the answer unless Mark formally submits his comment as an answer – Datadimension Aug 21 '16 at 23:29
-1

Pending Mark adding an answer this was what I had but it seems clumsy - I hoped there was a better way:

 $timebits = explode(":", $timepart);
 $now->setTime($timebits[0], $timebits[1], $timebits[2]);
Datadimension
  • 872
  • 1
  • 12
  • 31
  • the source code in the answer is incomplete. Define $now. Plus it relies on time in format HH:mm:ss – Alex P. Feb 20 '18 at 20:59
  • perhaps @AlexPandrea should read the question and write a constructive comment instead of trolling and downvoting - clearly requiring HH:mm:ss and also the PHP doc for 'setTime' which clearly refers to a DateTime object. Labelling the variable 'now' I think is self explanatory. – Datadimension Mar 27 '18 at 12:59
  • no intention to troll but since you answered your question and marked the answer as the accepted one, my opinion is that the solution quality and explanation could be better - as StackOverflow aims to high(er) quality answers. But you're right as well - nor I, nor other have produced better answers. It's a subjective view. – Alex P. Mar 27 '18 at 18:24
  • @AlexPandrea to be in a position to downvote you should also be in a position to either provide a 'better quality answer' that you speak of or correctly dispute my response to your initial comment. I think leaving downvote as is with no further constructive information, other than the contradiction that I am right but also being right is a subjective view does not increase StackOverflow quality in anyway whatsoever and also reflects badly on yourself. – Datadimension Mar 27 '18 at 18:58