Jump to content

doing the math


uknowho008

Recommended Posts

Date & time calculations in PHP are usually done using UNIX time stamps, which is the date/time as the number of seconds since 1-1-1970. If you can convert that number to the number of seconds since 1-1-1970, then the calculations are easy.

How did that number get into your database? What is the field type?

Ken
Link to comment
https://forums.phpfreaks.com/topic/15652-doing-the-math/#findComment-63809
Share on other sites

yes i realized later that i should be using unix time stamps. but i didnt really feel like converting everything in my database to unix timestamps. i dont understand why mysql uses this format if unix is normally used. the type field was set to TIMESTAMP >:(

so how do you suggest i convert my database to unix timestamp instead of this format then i guess?
Link to comment
https://forums.phpfreaks.com/topic/15652-doing-the-math/#findComment-63811
Share on other sites

The format of a timestamp field in MySQL  4.0 and earlier is YYYYMMDDHHMMSS, so the date string that is returned can be turned into a
"normal" date/time string by doing:
[code]<?php
$dt = "20060725202938";
$new_dt = substr($dt,0,4) . '-' . substr($dt,4,2) . '-' . substr($dt,6,2) . ' ' . substr($dt,8,2) . ':' . substr($dt,10,2) . ':' . substr($dt,12,2);
?>[/code]
The the date/time string for 30 minutes earlier can be gotten with:
[code]<?php
$last_30min = date('F j, Y G:i',strtotime('-30 minutes ' . $new_dt));
?>[/code]

Ken
Link to comment
https://forums.phpfreaks.com/topic/15652-doing-the-math/#findComment-63818
Share on other sites

well.. the problem with that is i need to compare the time from 30 minutes earlier to the times in the database. so i need to change the times in the database to unix timestamps instead of the way it is now. thanks for all the help though. im not mad im just frustrated. haha. it would all be so much easier if it just subtracted it for me. ughh..
Link to comment
https://forums.phpfreaks.com/topic/15652-doing-the-math/#findComment-63825
Share on other sites

Use mysql's own date and time functions!

On select, always use UNIX_TIMESTAMP, for easy use in php.

SELECT UNIX_TIMESTAMP(`somedatetimefield`) AS somedatetimefield

to for example select all id's of entries that where modified in the past 30 min:

SELECT id FROM somtable WHERE TIMEDIFF(NOW(),timestampfield)<MAKETIME(0,30,0)

http://dev.mysql.com/doc/refman/5.0/en/date-and-time-functions.html

Converting the columns to unix timestamps is not required, nor handy. You won't be able to use mysql's timestamp type  columns, you'd have to use php's time() and insert/modify it every time you alter a table.
Link to comment
https://forums.phpfreaks.com/topic/15652-doing-the-math/#findComment-63927
Share on other sites

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.