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

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;#');

Related

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.

Set correct reading format for a cell with PHPExcel

I'm using PHPExcel to read an Excel 2007 document. In this document there's a column with "Time" formatting and it displays data correctly in Excel. When I try to parse this column I get some weird values. Any idea how can I fix this?
Thank you.
When parsing, you need to specify the format of the column. e.g.
$my_cell = $objWorksheet->getCellByColumnAndRow(1, 1);
$my_cell_value = PHPExcel_Style_NumberFormat::toFormattedString($cell->getCalculatedValue(), 'hh:mm:ss');
print $my_cell_value;
Weird values?!? You mean you get a number instead of a human-readable date string.... it always helps to describe things accurately where possible, and weird isn't really an accurate description.
If you simply get the value from the cell, you'll be reading a raw timestamp value (MS Excel holds date and time values as a timestamp; like a Unix timestamp value in PHP, except that Excel's timestamp is the number of days since 1st January 1900 (or 1st January 1904, depending on which calendar it is configured to use).
MS Excel uses number format masking to display this timestamp as a human-readable date/time string.
You can use the getFormattedValue() method rather than simple getValue() to retrieve this as a formatted date/time (getFormattedValue() applies any number format mask for the cell to the value).
Not ethat if you've loaded the file with readDataOnly set to TRUE, then the number format masks aren't loaded, so PHPExcel cannot identify whetehr a cell contains a date or not.
Or, as James has suggested in his answer, you can convert the raw timestamp to a formatted value manually by applying number formatting with your own format mask
A third alternative is that you can use PHPExcxel's built-in date handling functions to convert this Excel timestamp value to a PHP/unix timestamp or to a PHP DateTime object (PHPExcel_Shared_Date::ExcelToPHP() and PHPExcel_Shared_Date::ExcelToPHPObject() respectively),
that you can then display using PHP's own date handling functions
The value for Time as it is stored in Excel is actually the fraction of the day, so 0.2 is 24 hours (or 1440 minutes or 86400 seconds) times 0.2. You can calculate the time of day based on that information, and then calculating from the beginning of the day. It makes it a little more usable than a formatted time, but a lot less readable.

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'.

How to convert microsoft access time format with 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.

Categories