Jump to content


Photo

doing the math


  • Please log in to reply
8 replies to this topic

#1 uknowho008

uknowho008
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 26 July 2006 - 04:26 AM

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

#2 uknowho008

uknowho008
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 26 July 2006 - 04:29 AM

please ignore the 1440 it should be 1800. haha. my bad

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 26 July 2006 - 04:40 AM

Where did the number in the $date variable come from?

Ken

#4 uknowho008

uknowho008
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 26 July 2006 - 04:43 AM

my database

#5 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 26 July 2006 - 04:48 AM

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

#6 uknowho008

uknowho008
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 26 July 2006 - 04:53 AM

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?

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 26 July 2006 - 05:10 AM

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:
<?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);
?>
The the date/time string for 30 minutes earlier can be gotten with:
<?php
$last_30min = date('F j, Y G:i',strtotime('-30 minutes ' . $new_dt));
?>

Ken

#8 uknowho008

uknowho008
  • Members
  • PipPipPip
  • Advanced Member
  • 55 posts

Posted 26 July 2006 - 05:33 AM

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..

#9 448191

448191
  • Staff Alumni
  • Advanced Member
  • 3,545 posts
  • LocationNetherlands

Posted 26 July 2006 - 10:57 AM

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...-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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users