0

A PHP/Laravel program of mine collects time dates from an API endpoint, in a variable that I'll call $time. It's presented in the following format:

2018-12-10T22:14:04Z

I want to tidy it up into something more typical, like any of the following:

"10-12-2018" "22:14, 10-12-2018" "10-12-2018, 22:14:04"

I'm new to this facet of PHP, so I've been researching different ways to do this. While I've come across some questions similar to mine, and I've found the documentation for tools like createfromformat and strtotime, but from what I've read I don't think they solve for the precise format I'm working with.

Are there any built-in ways in PHP that I can do this? Or any lines of code people might suggest I try to reformat the date as I want to? Any help on this would be greatly appreciated

  • That looks like an ISO 8601 date format so you *should* be able to use the [format 'c'](https://www.php.net/manual/en/function.date.php) with `DateTime::createfromformat()` – CD001 Jan 10 '20 at 11:51
  • You should be aware of the danger of 'tidying' date strings. '2018-12-10T22:14:04Z' has the very desirable property that *most* common software that deals with converting date strings into DateTime objects (whether in PHP, javascript, c#, whatever) will be able to understand that format. Whatever date string that you turn it into may not be as universally recognizable, and you may find yourself having to convert your new date format back into a more universal one later on. However, if it's just for display purposes and you're not planning on storing the data like that, then bon voyage. – TKoL Jan 10 '20 at 12:28
  • Why not parse the input string like explained by CD001 and then format it in the expected output format? What have you tried so far? Where are you stuck? – Nico Haase Jan 10 '20 at 12:46
  • @TKoL I understand, but I'm not worried about that. I'm not changing the actual property of the value, I'm just presenting it in another way on the front-end for the sake of presentation. – QuestionOverflow Jan 10 '20 at 14:40
  • @CD001 Could you give an example of what that might look like? e.g is it like "echo DateTime::createFromFormat('c', $time)" ? – QuestionOverflow Jan 10 '20 at 14:44
  • `echo (DateTime::createFromFormat('c', '2018-12-10T22:14:04Z'))->format('Y-m-d H:i:s');` ... in theory. **However** that's actually throwing an error on my dev box - and weirdly, if I do the reverse, take a date and format it to `c` (which replaces `Z` with `+01:00`) and put that exact string back in, it still fails to create the `DateTime` object. So it looks like you can format a date to ISO 8601 using `c` but **not** create a `DateTime object with createFromFormat('c', ...)` - the answer you've accepted works fine though :D – CD001 Jan 10 '20 at 15:36
  • I see! Thank you for the extra understanding! :D – QuestionOverflow Jan 10 '20 at 16:07

2 Answers2

3

You can pass your stored datetime string into DateTime() object and freely control format (and more) of it. For example:

$time = '2018-12-10T22:14:04Z';

$dateTimeObject = new \DateTime($time);
echo $dateTimeObject->format('Y-m-d H:i:s'); // will output: 2018-12-10 22:14:04
Tomasz
  • 4,847
  • 2
  • 32
  • 41
2

I would suggest that you use the Carbon package to convert dates and datetime it is really awesome.

Usually Laravel already contains Carbon but if you somehow do not have it install it using this:

composer require nesbot/carbon

Now you need to import for instance if you want to use it in your controller:

use Carbon\Carbon;

And you should be able to do something like this:

Carbon::createFromFormat('d/m/Y', $yourcurrentformat);

You should read the docs

utdev
  • 3,942
  • 8
  • 40
  • 70