php date conversion to date having time [closed]
-
07-02-2021 - |
문제
I am trying to convert php date conversion like 09/30/2011 to this format 2011-09-30 21:35:46.
I read some manuals but its going to be difficult for me.
$input = "09/30/2011";
$output = "2011-09-30 21:35:46";
$output = date('Y-m-d h:m:s', strtotime($input))
해결책
The format string 'Y-m-d h:m:s' should be modified to 'Y-m-d H:i:s' in your code.
In date
function, the format char 'm' is month, not minute; and 'h' is hour from 01 to 12, 'H' is hour from 00 to 23.
다른 팁
Your code works but if you want to play around with the DateTime in PHP here is a little example.
Every date output except a timestamp needs a timezone to get the right time in that timezone.
So, if your php config dosen't already set it up for you, set your default timezone by:
date_default_timezone_set('XXXX');
XXXX stand for a value out of the List of supported timezones
If you want to use your date as an object you need to initialize it now by:
$date = new DateTime();
$date will now have the current time, if you want to set a time in your example "09/30/2011" you can do this directly by writing:
$date = new DateTime('09/30/2011');
To format the date output you can use this:
echo $date->format('Y-m-d H:i:s');
Or if you want to set your time as well you can initilize the DateTime with a time as well:
$date = new DateTime( '09/30/2011 21:35:46' );
Always keep in mind that the formated output depends on your timezone.
To read more about DateTime look at the DateTime class manual.