Jump to content

Need help dealing with time and date.


Bikkebakke

Recommended Posts

Hi again, Freaks!

 

I stumbled upon a tiny-weeny problem I couldn't figure out myself, tried googling and searching for a while untill I decided to ask here, so:

 

How do you count how many seconds/minutes/hours etc. it's been since a certain date/time?

 

To be more specific, this is what I need to do:

[*]Fetch a datetime from database (YYYY-MM-DD hh:mm:ss)

[*]Count the minute difference between that value and current time

[*]Add the minutes to the database value (matches current date/time except for seconds)

-> This way I can store the minute difference in a variable and use it in a script AND update the datetime value in database without losing any seconds in that minute counting (since I only need to count the full minutes)

 

And I would also need to echo how many seconds left till next full minute.

 

 

Any help is appreciated.

 

~Bikkebakke

Link to comment
https://forums.phpfreaks.com/topic/199591-need-help-dealing-with-time-and-date/
Share on other sites

Ah, finally figured it out.

 

Incase anyone else faces the same problem:

 

I decided to convert the date/time to minutes and store in a bigint field in database.

To convert current time to minutes:

floor(time()/60);

To break it down:

floor() <- rounds the value down

time()/60 <- returns current time in seconds (seconds since 1970-something till present) and divides by 60 to convert the seconds to minutes.

 

 

 

To echo seconds till the next full minute:

echo date("s",60-time());

date() <- date function

"s" <- tells the date function to display seconds

60-time() <- 60-current time (in seconds due to the "s"-part)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.