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.
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.
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.
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;
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.
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.
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.
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));
Thats simple, use explode, try this below it wil work
$new_time = explode(" ",$timestamp);
$get_date = $new_time[0];
$get_time = $new_time[1];
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With