-1

I don't want to know how to use substr, there are many examples. But I want to know if it is the best way to short a string? Is there a better/more professional way to do this?

For example, this is what my print_r gives me. But I only need houres and minutes.

 string(25) "2020-03-05T12:00:00+01:00"
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
qws
  • 33
  • 6
  • 3
    it would be better for you to format that datetime then to work with it as a string – Zeljka Mar 05 '20 at 12:50
  • Does this answer your question? [PHP, How to get current date in certain format](https://stackoverflow.com/questions/5206829/php-how-to-get-current-date-in-certain-format) – yivi Mar 05 '20 at 15:40

1 Answers1

4

You could use this piece of code :

<?php

$time = "2020-03-05T12:00:00+01:00";
$dt = new DateTime($time);
echo $dt->format('H:i');

Doing this allows you for many advantages. You can, for instance, manipulate dates more easily (adding, substracting time intervals...) and format the result the same way.

Altherius
  • 754
  • 7
  • 23