Jump to content

Recommended Posts

For goofy reasons I won't explain, (legacy code) I need to have a separate column in my database with a human readable time. (can't strtotime)

 

In other words, I have 'time' already and it has values like 1815 or 1100 etc... I added an 'hrtime' to the db and now I need to take the 'time' run it thru a function and put the value in hrtime.

 

Example I have:

event      time

-----------------

Picnic    1300

Party      1930

 

and I need

 

event    time    hrtime

--------------------------

Picnic    1300    1:00 pm

Party    1930    7:30 pm

 

 

Here is the function to convert the time from military to standard in case you care:

// function to convert mil time to standard time
function miltostandard($mtime)
{
$milTime = $mtime;
$amPm = 'am';

if (intval($milTime > 2359) || strlen($milTime) < 4)
{
echo $time . "<br />";
die("Please use a valid time");
}

$milTimeHours = substr($milTime, 0, 2);
$milTimeMinutes = substr($milTime, 2, 2);

if (intval($milTimeHours >=12))
{
$amPm = 'pm';
if (intval($milTimeHours > 12))
{
$standardTimeHours = intval($milTimeHours - 12);
}
else
{
$standardTimeHours = intval($milTimeHours);
}
}
elseif (intval($milTimeHours == 0))
{
$standardTimeHours = 12;
}
else
{
$standardTimeHours = intval($milTimeHours);
}
$standardTime = "$standardTimeHours:$milTimeMinutes $amPm";
return $standardTime;
} 

 

I just need to loop through the database and apply the function.

 

thanks

A) Store the values as a TIME data type (you can use the mysql STR_TO_DATE() function to covert your existing values).

 

B) You can retrieve TIME data type values in any format you want using the mysql TIME_FORMAT() function.

 

A -

 

STR_TO_DATE(your_existing_time_column,'%H%i')

 

B -

 

TIME_FORMAT(a_TIME_type_column,'%l:%i %p')

Hi

 

Possibly a bit OTT.

 

UPDATE timetable
SET hrtime = if(timefield >= 1200,
CONCAT(FLOOR((timefield-1200)/100),':',SUBSTR(CONCAT('00',cast(timefield - (FLOOR((timefield)/100))*100 AS CHAR)),-2),' pm'),
CONCAT(FLOOR((timefield)/100),':',SUBSTR(CONCAT('00',CAST(timefield - (FLOOR((timefield)/100))*100 AS CHAR)),-2),' am'))

 

All the best

 

Keith

Can you change the table that the code reads from? And without having to change the table it writes to as well?

Or simply modify the SELECT query?

 

No, the problem is we have one legacy system... then we have a new reporting tool. The reporting tool simply dumps the content of the field... it's a reporting tool not a data manipulation tool... it just reports what's in the db, it does not change it.

 

So it reports the time, but it is in mil time. After talking to the developer of the reporting tool, changing it is monumental. The easiest route is to make another hrtime field and store human readable text there. 

 

So I have the function to convert it, I just need to read one field, convert it and store the result another field....

 

I thought my way was pretty simple but apparently not. ;) I'll look at the stuff above and see if it help.

Hi

 

Possibly a bit OTT.

 

UPDATE timetable
SET hrtime = if(timefield >= 1200,
CONCAT(FLOOR((timefield-1200)/100),':',SUBSTR(CONCAT('00',cast(timefield - (FLOOR((timefield)/100))*100 AS CHAR)),-2),' pm'),
CONCAT(FLOOR((timefield)/100),':',SUBSTR(CONCAT('00',CAST(timefield - (FLOOR((timefield)/100))*100 AS CHAR)),-2),' am'))

 

All the best

 

Keith

 

This worked fantastically well. I'd love to learn how to make it a function in the db because that looks ubber cool... but for now this saved the day. Gracias Kickstart.

The built-in date/time functions can already do that.

 

Indeed! I started with Kickstart's solution because it was on top and I was working my way down. ;)

I see what you are saying, your way is cleaner.  Well, I wrote it in php and called it before the report runs and it works fine... phpmyadim says it took 0.027 second to change the whole db so I'm just going to leave it, but your way is "more betterer." Thanks

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.