Jump to content

Archived

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

uknowho008

doing the math

Recommended Posts

so im trying to work with dates. im trying to make a variable with the timestamp for 30 minutes ago. how do i make it give me an integer instead of somthing like this 2.00607252015E+013?

this

$date = "20060725202938";
$last_30min = $date - 1440;
echo $last_30min;

outputs this

2.00607252015E+013

Share this post


Link to post
Share on other sites
Where did the number in the $date variable come from?

Ken

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
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?

Share this post


Link to post
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

Share this post


Link to post
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..

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.