123
-=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- (c) WidthPadding Industries 1987 0|614|0 -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=- -=+=-
Socoder -> Off Topic -> PhP - Time Difference

Sat, 29 Jul 2017, 11:11
GfK
gah. PHP does my fucking box in! All I want to do is get the difference (in hours) between two DateTimes. Will it do it? Will it fuck!
Sat, 29 Jul 2017, 11:11
Jayenkai
Strtotime both times to get UnixTimes for both, then subtract and divide by ...um.. 60x60, IIRC

-=-=-
''Load, Next List!''
Sat, 29 Jul 2017, 11:49
GfK
Shall try in a bit. All it is... i have a thing on my site where people can contact, but it emails them for verification first. The database had over 500 unverified spam entries in (when a user verifies, their entry gets deleted), so i figured every time a new spam appears, I'd purge any of the others at the same time that are older than X days.

Simple.... but apparently not!! Fair do's I haven't done any PHP in years.

|edit| some of the spam related to very sinister shenanigans which i do not care to see, and would call the police if i thought that the sender was in any way identifiable so its important i get this self-purging thing going.
Sat, 29 Jul 2017, 13:42
GfK
OK... any clues what I'm doing wrong here? This code:


...spits out the following:

would delete record 617, age 0
would delete record 616, age 0.0477777777778
would delete record 615, age 0.0713888888889

Why? All three are less than 48?!

|edit| how odd.... made a little change to only calculate the time difference once, and it works now??


Sat, 29 Jul 2017, 14:05
Jayenkai
In the first instance, you've put a ; at the end of the if line, thus ending the if there.. the bracketted stuff then "just happens" regardless of what the If did.

-=-=-
''Load, Next List!''
Sat, 29 Jul 2017, 14:11
GfK
Oh bollocks!

Thanks for your help. Works a treat now.
Sat, 29 Jul 2017, 14:52
Jayenkai
Odd that it allowed completely superfluous brackets, though. Damn thing's always whinging about brackets to me!

-=-=-
''Load, Next List!''
Sat, 29 Jul 2017, 15:19
cyangames
An alternative way to get around it is with the mktime function, really helps with timestamps, for example, if I wanted to delete records aged 1 week ago I'd use someting like the following:



mktime can make light work of date based issues.

|edit| I should probably mention that db_query isn't an actual function name, just one that I've used for ages as a wrapper. |edit|

-=-=-
Web / Game Dev, occasionally finishes off coding games also!