Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting Datetime into a date and a time value

Tags:

php

mysql

Can someone give me a quick and dirty way to split a datetime (28-1-2011 14:32:55) into just the date (28-1-2011) and the time ( 14:32 ) or even better (2:32 PM) using PHP. Using a mySQL database as well.

Cheers

like image 813
jose quervo Avatar asked Apr 03 '11 19:04

jose quervo


People also ask

How to split date and time with text to column?

Split date and time with Text to Column. In Excel, you also can use Text to Column to split the Date and Time column to columns. 1. Select the data range and click Data > Text to Columns, then in the popping dialog, check Delimited option.

How do you pull the time between a date&time?

If a cell contains a combined date and time, you can use the INT function to pull the time value into a separate column. Dates are stored as numbers in Excel, with the decimal portion representing the time. To calculate the time value, subtract the date integer value from the combined date and time. The remaining decimal portion is the time.

How do you separate date and time in Excel?

Use the INT Function to Split Date and Time Excel considers dates as integer numbers (starting from Jan 1, 1900) and times as fractions. So, we can separate the date part from the data using the INT function, then subtracting this result from the original data will return the time in Excel’s numeric format.

How to split a column by a given value in Excel?

Then in the first cell of the Date column (except the header), type this formula =INT (A2) (A2 is the cell you need to split by), then drag the fill handle to the range needed to apply this formula. See screenshots: 5.


1 Answers

If you're using PHP > 5.2:

$myvalue = '28-1-2011 14:32:55';

$datetime = new DateTime($myvalue);

$date = $datetime->format('Y-m-d');
$time = $datetime->format('H:i:s');

Prior to PHP 5.2 mhitza gave a good answer.

like image 66
Dan Soap Avatar answered Sep 21 '22 18:09

Dan Soap