Extracting time only from mysql datetime feild?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Extracting time only from mysql datetime feild?

Post by robster »

Hi all,

I have this piece of code for getting a mysql date and making it look pretty (real words) in php, donated by a kind soul in this forum:

Code: Select all

function convert_date_time($date)
	{
		$date = "$date";
		$parts = explode( "-", $date );
		$timestamp = mktime( 0, 0, 0, $parts[1], $parts[2], $parts[0] );
		$return_date = date( "l, d M Y", $timestamp );
		return ($return_date);
	}
That is fine with a mysql date format (0000-00-00);


I now have a mysql datetime format (0000-00-00 00:00:00) and want to do the same thing except only extract the time from the data. I've been playing around but to no luck :(

I'm wondering if somebody could help out? This is what I have so far, and it fails, it shows 12:00 all the time.

Code: Select all

function convert_date_time($date)
	{
		$date = "$date";
		$parts = explode( "-", $date );
		$timestamp = mktime( 0, 0, 0, $parts[1], $parts[2], $parts[0] );
		$return_date = date( "g:i", $timestamp );
		return ($return_date);
	}
Any help gratefully accepted!

Rob

:)
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

your new function is exploding on "-", which will give you "00", "00", "00 00:00:00" as far as I'm aware.

explode on ":" and you will get "00-00-00 00", "00", "00".

so you could explode on ":" as said above and then trim the first 9 characters from the first entry in the array.

so you could do this:

Code: Select all

$parts = explode(":", $date);
$hour = substr(9, $parts[0]);
$minute = $parts[1];
$second = $parts[2];
$time = $hour . ":" . $minute . ":" . $second;
lol, just looking back on that, you could do it like this...

Code: Select all

$parts = explode(" ", $date);
$time = $parts[1];
so try:

Code: Select all

function convert_date_time($date) {
  $date = "$date";
  $parts = explode(" ", $date);
  $time = $parts[1];
  return ($time);
}
HTH.
User avatar
robster
Forum Contributor
Posts: 360
Joined: Wed Jul 16, 2003 8:28 am
Location: Sunshine Coast, Australia

Post by robster »

totally perfect!

A silly question as I'm new to this kind of explode array stuff, but how did you know time would be in '1' of the array?
User avatar
jayshields
DevNet Resident
Posts: 1912
Joined: Mon Aug 22, 2005 12:11 pm
Location: Leeds/Manchester, England

Post by jayshields »

i knew because when you feed something into an array, the first entry will be in $var[0], the second entry will be in $var[1], and so on.

so for example, if you exploded "jay, rob, fred, bob" into an array with ", " as the string to explode on, jay would be indexed as 0, rob as 1, fred as 2 and bob as 3.

glad it worked btw :)
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post by Jenk »

for future reference, sscanf is a wonderful function for this sort of purpose:

Code: Select all

list($month, $day, $year) = sscanf("%d-%d-%d 00:00:00", $date);
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

what's wrong with using DATE_FORMAT() ?
Post Reply