How to convert microsoft access time format with php - php

Can someone please help me with the following.
I am using php and sql to query a microsoft access database. I have a Date/Time field called "StartTime" that is set to "General date" format.
When inspecting the field from within the microft access GUI the date appears as "08:45:00".
My problem is when I retrieve this data with my php and sql and then display it in then write it to the browser it appears as follows "1899-12-30 09:00:00" (let's call this 'long date format' for what i'm about to say).
I have tried messing around with the php date function using the 'long date format' as the second argument but I can't seem to get it to display in the browser in the format "08:45:00".
Please can someone tell me what I need to do.
Thanks

The problem is that MS Access stores the date as a double with fractions. So you need to parse it accordingly.
The whole part of the number is the amount of days from Dec 30 1899. The fractional portion is the fraction of the day. So you need to multiply the fraction by 24 to get hours, multiply the resulting fraction by 60 to get minutes, and multiply the resulting fraction by 60 to get seconds.
Alternatively, you can multiply the fraction by 86400 (number of seconds in 24 hours), and then use a PHP function to convert the seconds into the time of day.
Check out this article:
http://support.microsoft.com/kb/210276

You could ask Access' db engine to give you only the time portion of those Date/Time values as formatted strings:
SELECT Format(StartTime, "hh:nn:ss") AS time_only
FROM YourTable;
That way you wouldn't have to transform the values in PHP ... just use them.

Related

Convert [h]:mm:ss;# format to Excel number

When I paste 41:30:00 into a cell of Excel, with "General" format option selected, it returns me this decimal: 1,729166667
How do I achieve this decimal through PHP (formula)?
I mean, I want to set the cell with the value of 1,729166667, not 41:30:00 (PHPExcel), because I getting problems with formatting and being able to read it as a value in the Excel file.
Just figured it out.
The number generated from an amount of time, in this case, is represented as a fraction of 24 hours.
So, 12:00:00 will be 0,5 and so on.
I converted the number to seconds, then divided it by 86400 which is 24h in seconds.
Also, used this custom format for PHPExcel to display the time correctly:
$objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.$row_start.':E'.$row)
->getNumberFormat()->setFormatCode('[h]:mm:ss;#');

Inserting actual hours (not time) to MySQL

I am trying to insert actual hours not the time itself to MySQL database through form fields. So for example
$time1 = '00:00';
$time2 = '27:20';
$time3 = '00:45';
So I can retrieve the different rows and can calculate on the fly whenever require. Either through search query or even in other area of the system.
When I have tried to do addition of above three times, it is not giving the result the way I am looking for
$total = strtotime($time1) + strtotime($time2) + strtotime($time3);
echo date('H:i:s', $total);
The result
14:16:44
While it should be something like
28:05:00
I have used TIME DATATYPE in MySQL table. I may use as a TEXT but I am also concern about the error happen in user input. Where I do not have to force the user to insert the any particular format but they can either insert as below way
27.20
27:20
or
1.5
1:30
My main concern is to calculate the time, the user input can be on second priority but it would be great if can implement.
So is there anyway, idea or hint to achieve this?
date() expects the timestamp in UNIX format, i.e. seconds since January 1 1970 00:00:00 UTC (which is also the value provided by strtotime)
You're passing it the result of adding a series of amounts of time since 1 January 1970 instead of just adding up hours, so (as far as date is concerned) you're generating a random date and time, and printing only the time (try printing the date of $total and see what you get).
Since your time is stored in the database, one possibility is to let MySQL handle the time calculations itself, e.g.:
SELECT ADDTIME('00:00',ADDTIME('27:20','00:45'))
will result in "28:05:00". You can have your database fields as TIME and operate on them directly through SQL, and do the user input conversions into acceptable TIME values in PHP.
If you're only interested in the hours and minutes, why don't you just store the value as an in integer? Just multiply the hours by 60.
You can handle the conversion in PHP.
Alternatively, you can also easily use two (very small) int fields for this.

Excel Custom Time Format Decode in PHP

I've got ton of spreadsheets I'm processing in PHP and one of the cells contain a timestamp which has a custom formatting of: [h]:mm:ss.0;#
This turns a number such as 2.09491E-05 into 0:00:01.8
When I read it into php I can only get the value of 2.09491E-05 but I don't know how to convert this into seconds. Can anyone help?
In Excel, one day equates to 1. So in order to convert the Excel representation of 1.81 seconds, 2.09491E-05, into a number where each second equals 1, just multiply by the number of seconds in a day, i.e., 24*60*60, or 86,400:
2.09491E-05 * 86,400 = 1.81

CAST HEX as DATATIME - how to get date?

I have a problem with processing a SQL Server database dump in PHP.
I have one column called datatime with values like :
0x0000a0af00d7f2eb
I need to extract, in PHP, the date and time values of this column. I don't have SQL Server available so I can't use the obvious solution of CAST(0x0000a0af00d7f2eb AS datetime).
Someone has told me that this hex: 0000a0af00d7f2eb is created by 4 bytes of date and 4 bytes of time.
So I know that:
When I will change 0000a0af (first 4 bytes) to decimal I will get number of days from 1900. That works fine.
But when I'm trying to change last the 4 bytes (so there should be time) : 00d7f2eb to decimal I'm getting something which I can't understand. It should be a time from midnight in milliseconds and sometimes this value is ~3 times lower.
Could anyone help in converting 0000a0af00d7f2eb to date? I know that time is between 5 AM and 11 PM, and the day is in last week.
According to the linked article in the other question linked to by Rene, SQL Server stores 3.33 millisecond intervals in the second set of 4 bytes, not milliseconds. So if you're calculating with milliseconds, you will indeed be getting a time about 1/3 of what it should be. Using your example, let's start by converting to decimal
00d7f2eb -> 14152427 3.3ms intervals
Now multiply out by 3.3 to convert to milliseconds, and divide by 1000 to get seconds
14152427 * 3.3 / 1000 ~ 47127.58
So this represents about 47 thousand seconds after midnight. Dividing by 3600 seconds in an hour
47127.58 / 3600 ~ 13.091
So this represents a time of about 13.1 hours after midnight, which agrees with the result of the cast done in SQL Server.
select CAST(0x0000a0af00d7f2eb AS datetime) as t
is working fine for me. and it returns 'August, 16 2012 13:06:14-0700'.

Merging 2 int type strings into one UNIX timestamp

I have a table with 2 rows, one of which represents date and the other time. These rows are not date format; they are int type and I can't change the original rows' type. The date entries are written as 20120306, and the time entries are written, for example, 13000 for 01:30 UTC in 5 digits, and 130000 for 13:00 UTC in 6. What I need to do is put both strings (date and time) into 1 UNIX timestamp. I can either use this in a php script or merge them into a new table, whichever works best. The problem for me is the php mysql syntax, functions and sequence for putting these 2 odd strings together into one timestamp.
If I can figure out how to do this, it would help me solve a whole mess of inaccuracies in a calendar reminder script I am trying to put together. I have tried configuring these strings into times separately in the queries, but no matter how it's filtered, the outcome is right in some circumstances and wrong in others due to the nature of the original program. If I had a PHD in PHP I would rewrite the entire program but I don't. I am a major newbie at this. So I just need to write my little PHP scripts to utilise what has already been provided. Any help in my learning quest would be appreciated.
The function strtotime() can also read compound formats.
If you take a look at that list you will notice that your format is a close match to the "XMLRPC (Compact)" listed there.
It's your date string concatenated with a "t" and the time string. So, after you expanded your time string to length 6 (you already know how to do that), you can produce such a string and strtotime() will output your Unix time stamp:
$dt = "20120306";
$tm = "130000";
$unixTime = strtotime($dt . "t" . $tm);

Categories