Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Hour and minute from time string

Tags:

date

php

I have response from an API like below

2019-05-08T10:36:00+0530

I want to take 10:36 from it, right now I do something with regex like

preg_match('/T(.*?)\:00/');

It works, but is there any other actual way to do it more efficiently? That works on any time string in this format?

like image 469
Gracie williams Avatar asked Nov 27 '25 02:11

Gracie williams


1 Answers

Create a DateTime object by passing that string into the constructor and just format it to your liking with format()! You might want H:i if you just want the hours and minutes, or H:i:s if you want to include the seconds.

$string = "2019-05-08T10:36:00+0530";
$date = new DateTime($string);
echo $date->format("H:i:s");
  • Live demo at https://3v4l.org/BmnoU
like image 54
Qirel Avatar answered Nov 29 '25 19:11

Qirel