Reputation: 151
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
Upvotes: 15
Views: 46995
Reputation: 41
maybe you can split a datetime string into date and time components using the date()
and strtotime()
functions along with DateTime
objects for better formatting. (=
$datetimeString = '28-1-2011 14:32:55';
// Splitting into date and time
$date = date('d-m-Y', strtotime($datetimeString)); // Date format: 28-1-2011
$time24 = date('H:i', strtotime($datetimeString)); // Time format (24-hour): 14:32
$time12 = date('h:i A', strtotime($datetimeString)); // Time format (12-hour with AM/PM): 02:32 PM
echo "Date: $date<br>";
echo "Time (24-hour format): $time24<br>";
echo "Time (12-hour format): $time12";
strtotime()
converts the string to a Unix timestamp.
date()
formats the timestamp into the desired date or time format.
d-m-Y
in date('d-m-Y')
represents the day-month-year format for the date.
H:i
in date('H:i')
represents the 24-hour format for the time (hours:minutes). h:i A
in date('h:i A')
represents the 12-hour format with AM/PM.
Upvotes: 0
Reputation: 135
One simple instruction will do the trick
explode will transform datetime to an array
and list will sort the datetime array into its needed values
$datetime = "28-1-2011 14:32:55";
list($date, $time)=explode(' ', $datetime);
// check the result
echo "date:". $date;
echo "<br>time:". $time;
// further more you can easily split the date into
// year month and day
list($year, $month, $day)=explode('-', $date);
Upvotes: 2
Reputation: 137
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 output will be- 14:32:55(Here we split the time from the DateTime)
We can covert the time to am and pm format also
select SUBSTRING_INDEX("28-1-2011 14:32:55", " ",-1) into @timeOnly;
SELECT TIME_FORMAT(@timeOnly, "%h %i %s %p")into @time;
select @time;
The time format will become 02 32 55 PM
Upvotes: 0
Reputation: 533
if you want to parse in the date from your Mysql and you want to remove time then you can use this function
$date1=date("Y-m-d",strtotime('$your database field'))
Upvotes: 0
Reputation: 25604
if your source of data is MySQL:
SELECT DATE( date_field ) AS date_part, TIME( date_field ) AS time_part ....
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_time
http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html#function_date
Edit : to answer the question from comments (example):
WHERE DATE( date_field ) > '2017-01-01'
Upvotes: 10
Reputation: 1095
If you looking for a really quick and dirty solution.
$datetime = "28-1-2011 14:32:55";
$date_arr= explode(" ", $datetime);
$date= $date_arr[0];
$time= $date_arr[1];
Upvotes: 0
Reputation: 10248
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.
Upvotes: 26
Reputation: 5715
In php you can use the date
and strtotime
functions for easy extraction.
$datetime = "28-1-2011 14:32:55";
$date = date('Y-m-d', strtotime($datetime));
$time = date('H:i:s', strtotime($datetime));
Upvotes: 9