Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: splitting the date and time within a timestamp value in mysql

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

I would like to split apart and then fetch the date (YYYY-MM-DD) in a variable and also 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 out with this using php.

like image 600
Sms Avatar asked Jan 22 '13 11:01

Sms


People also ask

What is the format of timestamp in MySQL?

MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

What is timestamp format in PHP?

Summary. The date function in PHP is used to format the timestamp into a human desired format. The timestamp is the number of seconds between the current time and 1st January, 1970 00:00:00 GMT. It is also known as the UNIX timestamp. All PHP date() functions use the default time zone set in the php.ini file.

How to split datetime (28-1-2011 14-32-55) into date and time in MySQL?

We can easily split DateTime (28-1-2011 14:32:55) into date and time in MySQL. select SUBSTRING_INDEX ("28-1-2011 14:32:55", " ",1) into @dateOnly; select @dateOnly; The output will be- 28-1-2011 (Here we split the date from the DateTime) select SUBSTRING_INDEX ("28-1-2011 14:32:55", " ",-1) into @timeOnly; select @timeOnly;

How do I convert a string to a timestamp in PHP?

The almost magical function strtotime () takes a string of date/time formats as its first argument, and a Unix timestamp to use as the basis for the conversion. See the documentation for acceptable date formats. PHP’s DateTime object is the object-oriented approach to dealing with dates and time zones.

What is MySQL datetime value?

The DATETIME value is the local time of the server running MySQL in whatever time zone that is. If all you are ever doing only concerns one server in one time zone, DATETIME will probably fit most of your needs and I am highly jealous of you.

How to use default date and time format in MySQL using PHP?

However, it can be used with any time format functions to change it and display it. When writing a query in MySQL using PHP it’s applicability will be checked on the basis of MySQL itself. So use default date and time format as provided by MySQL i.e. ‘YYYY-MM-DD’ Example 2: PHP program to insert date into the table.


3 Answers

You could simply split the string by the space character using PHP's explode() function -

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

Another way of doing this would be this would be to use strtotime() combined with date()-

$date = date('Y-m-d',strtotime($timestamp));
$time = date('H:i:s',strtotime($timestamp));
like image 56
Lix Avatar answered Oct 26 '22 15:10

Lix


Thats simple, use explode, try this below it wil work

$new_time = explode(" ",$timestamp);
$get_date = $new_time[0];
$get_time = $new_time[1];
like image 30
Php Geek Avatar answered Oct 26 '22 16:10

Php Geek


You could use the explode function; plus list function could make your code slightly shorter:

list($get_date, $get_time) = explode(" ", "2012-10-19 18:19:56");
var_dump($get_date, $get_time);
like image 30
Salman A Avatar answered Oct 26 '22 15:10

Salman A