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
Related
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;#');
i am trying to use two unix time stamps to determine the time in days between the two.
I am using this:
$days_Since_Updated = ceil(abs(time() - $lastupdated) / 86400);
The last updated variable is saved in my database as a unix stamp aswell.
This code works perfectly fine, but only shows 1 as a minimum number.
How can i have it so that the code returns a number that can be 0? Currently it will show 1 if the number is actually less than a day difference between the two.
I know ceil rounds up, not sure whats another better method is? Thanks!
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.
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'.
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.