-4

I have this DateTime format:

2020-08-27 19:00:00

and I want convert to this format:
Thu Aug 27 2020 19:00:00 GMT-0500 (hora estándar de Colombia)

my version php is 5 can i how to do thanks

HarisH Sharma
  • 1,101
  • 1
  • 11
  • 38
avilac3
  • 3
  • 3
  • Any code attempt? Neither are formats. Just are how you want the data displayed. So your code attempt will help on providing some suggestions. – GetSet Aug 28 '20 at 04:29
  • no have php code, i have the Date and i need convert to this format. – avilac3 Aug 28 '20 at 04:48
  • Its not a "format" as mentioned. It's how the data is displayed. I guess if you insisted you could call that a format. Like "Mr. Joe Smith", "Joe", "Mr. Smith", and "Joe Smith" are all "formats", I guess. .... But without code to go by on what you intend, no idea what you are talking about. Sorry. – GetSet Aug 28 '20 at 04:50
  • `DateTime` is a class that can be an object. Yes it can be displayed in certain "formats". Where's your code? ... I see what you mean now by "format". But im not seeing any code. Hence my earlier confusion. – GetSet Aug 28 '20 at 04:52
  • in javascript the code is ` startTime = new Date( Date.UTC( date.getUTCFullYear(), date.getUTCMonth(), date.getUTCDate() ) ); ` – avilac3 Aug 28 '20 at 04:56
  • But you state `php` as your language. Truly lost now. – GetSet Aug 28 '20 at 04:57
  • this code javascript is the idea to php made console.log and see the format to i want – avilac3 Aug 28 '20 at 05:17

1 Answers1

0

I'm gonna throw you a bone here because dates are hard. You can use the basic date function and strtotime to do everything you need, the trick is figuring out which formatting flags to use. You're gonna have a bad time with timezones, so be prepared to tear some hair out over that. Your date input doesn't have any timezone info, so your output is going to use whatever is set in your PHP instance.

Good luck.

<?php
$dateInput = '2020-08-27 19:00:00';

$time = strtotime($dateInput);

/*
Thu Aug 27, 2020 19:00 EDT-0400 (America/New_York)
The timezone will depend on what's set in your PHP instance. Look at date_default_timezone_set
*/
$output = date('D M j, Y H:i TO (e)', $time);
echo $output.PHP_EOL;

/*
Thu Aug 27, 2020 19:00 EDT-0400 (hora estándar de Colombia)
"hora estándar de Colombia" isn't a thing that php dates can do, so you have to hard code it.
Escape all non-format characters. Some are safe, but let's just nuke it from orbit. If this is more than
a one-off, write a function to escape the fancy timezone string.
*/
$output = date('D M j, Y H:i TO \(\h\o\r\a\ \e\s\t\á\n\d\a\r \d\e \C\o\l\o\m\b\i\a)', $time);

echo $output.PHP_EOL;
Rob Ruchte
  • 3,569
  • 1
  • 16
  • 18