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!
Related
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.
Actually I working on a php website where User ads some entries in to database. At the time of insertion he adds one date field which also stored into db. Now I want the the age (how much time elapsed after saving that entry into database) of that entry.
For that I calling one function to which I am passing that date & then using strtotime() function I am getting that time in seconds. For getting the elapsed time I am subtracting that converted time from current time.
And here I am getting the main problem. It gives me NEGATIVE value. And I am stuck here so long & I am unable to figure it out why its returning negative value. Please help me out in this. Here is my tried code.
function timeSpan($string)
{
$timestamp = strtotime($string);
$now = time();
$timeSpan = $now-$timestamp;
...........
...........
}
Here I am passing the datetime as $string to the function which is saved in the db at the time of form submitting. I have saved the date with type "datetime". And now the $timeSpan variable is returning a negative value. Anybody have any idea why its returning negative value?
If your substraction is returning a negative value, its because the time() stored is somehow newer than the current time in your server.
time() is the amount in seconds passed since 1/1/1970.
If your saved timestamp is, for example, 111 and your server says that current time is 100, you will get a negative value.
Hey guys I got the solution for this. Actually the function returns the server time. And that time is dependent upon where the server is located. Its returns that time. So I specified my timezone. And now its returning correct time.
This seems like there should be a very easy solution, however everything I'm trying is either giving me an error or the wrong result.
I'm pulling data from a MySQL table and the time is stored in the Epoch format in the database. When I make the query on the website it's showing: 3672 (the same number shown in the database). I've tried using the date() function, a number of different str* functions, different arithmetic operations, however nothing is giving me the actual time, which should be showing as: '1:02'.
I'm not trying to pull the date, actual time, etc. I'm just trying to convert an Epoch time string to a traditional 'H:mm' format, because these are for durations, not timestamps.
Any help would be greatly appreciated!
As others already noticed, this is not really any standard epoch time but simply number of seconds since midnight.
Looking at your example, you only need hours and minutes (rounded up). This will give you those:
$h = (int)($number / 3600);
$m = ceil(($number - $h * 3600) / 60);
$result = sprintf('%d:%02d', $h, $m);
Dividing number by 3600 (60 for seconds * 60 for minutes) will give you number of hours. Using your example of 3672 this will give you 1.
To get minutes you just remove hours to get seconds (72) and then divide that by 60 to get minutes (1 minutes and 12 seconds). Since your example specifies 1:02 is result, you can simply take next upper integer (2).
At end result is 1:02, as you specified.
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'.
I am converting PHP (v 5.2.17)-based reports to SSRS.
I'm trying to write a query that subtracts dates like the php file does.
The date calculation either matches exactly, or it differs by exactly 3600 seconds
$timediff=strtotime(date("Y-m-d"))-strtotime("03/29/2007");
print $timediff; // 137635200
select (trunc(sysdate) - to_date('03/29/2007','MM/DD/YYYY'))*60*60*24 from dual
-- returns 137635200 - matches
$timediff=strtotime(date("Y-m-d"))-strtotime("11/23/2009");
print $timediff; // 53823600
select (trunc(sysdate) - to_date('11/23/2009','MM/DD/YYYY'))*60*60*24 from dual
-- returns 53827200 - doesnt match - off by 3600
I've searched stackoverflow and found the following example which sounds like it (11/23/2009 is a monday, and so is today 8/8, but it doesnt differ in the same way) PHP Strtotime erratic function
My assumption is that the php calculation is wrong, and the oracle is correct.
What say you? ;-)
Thanks!
Without seeing the exact values I suspect its because one of them is adjusting for daylight savings and the other is not. Most parts of the world change to/from daylight savings time during march. So in your first example both dates are in the same GMT offset, but in the second (November) they're not.
So one of your platforms (probably Oracle) is taking this one hour shift into account and the other is not.
3600 = an hour so could be timezone differences?
53827200 = ( 623 * 60*60*24 )
So 53827200 is exactly 623 days of 24 hours.
53823600 is 622.958333 days or 622 days (of 24 hours) plus 23 hours.
Both are right, for a given definition of right.