PHP: splitting date and time by timestamp value in mysql

I have a field called "timestamp" in the database table that saves the value in this format: - YYYY-MM-DD HH: MM: SS.

I would like to split and then get the date (YYYY-MM-DD) in the variable, as well as the time (HH: MM: SS) in another variable. Example:

$timestamp = "2012-10-19 18:19:56"; $get_date = "2012-10-19"; $get_time = "18:19:56"; 

I would be glad if anyone can help with this using php.

+4
source share
6 answers

You can simply break the string with a space character using the PHP function explode() -

 $timestamp = "2012-10-19 18:19:56"; $splitTimeStamp = explode(" ",$timestamp); $date = $splitTimeStamp[0]; $time = $splitTimeStamp[1]; 

Another way to do this would be to use strtotime() in combination with date() -

 $date = date('Ym-d',strtotime($timestamp)); $time = date('H:i:s',strtotime($timestamp)); 
+10
source

It's simple, use a blast, try it below. wil work

 $new_time = explode(" ",$timestamp); $get_date = $new_time[0]; $get_time = $new_time[1]; 
+3
source

You can use the explode function; plus the list function can make your code a little shorter:

 list($get_date, $get_time) = explode(" ", "2012-10-19 18:19:56"); var_dump($get_date, $get_time); 
+3
source
 $timestamp = "2012-10-19 18:19:56"; $splits = explode(" ",$timestamp); $get_date = $splits[0]; $get_time = $splits[1]; 
+1
source

instead of exploding, you can just do it like this. This will help you in whatever format you have.

 echo $timestamp = "2012-10-19 18:19:56"; echo '<br>'; echo $data = date('Ym-d',strtotime($timestamp)); echo '<br>'; echo $time = date('H:i:s',strtotime($timestamp)); 

Out put

 2012-10-19 18:19:56 2012-10-19 18:19:56 
0
source

check this php script:

http://phpfiddle.org/lite?code=%3C?php \ n \ n $ datetime% 20 =% 20% 2228-1-2011% 2014: 32: 55% 22; \ NLIST ($ dates,% 20 $ time) = explode (% 27% 20% 27% 20 $ DateTime); \ n \ n //% 20check% 20the% 20result \ Necho% 20% 22date: 22%.% date $ 20; \ Necho% 20% 22% 3Cbr% 3Etime:% 22.% 20 $ time; \ n \ n //% 20further% 20more% 20you% 20can% 20easily% 20split% 20the% 20date% 20into \ n //% 20year% 20month% 20and% 20day \ NLIST ($ year,% 20 $ month,% 20 $ per day) = explode (% 27-% 27,% 20 $ date)? \ N \ n% 3E \ n

0
source

All Articles