uknowho008 Posted July 26, 2006 Share Posted July 26, 2006 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 this2.00607252015E+013 Link to comment https://forums.phpfreaks.com/topic/15652-doing-the-math/ Share on other sites More sharing options...
uknowho008 Posted July 26, 2006 Author Share Posted July 26, 2006 please ignore the 1440 it should be 1800. haha. my bad Link to comment https://forums.phpfreaks.com/topic/15652-doing-the-math/#findComment-63802 Share on other sites More sharing options...
kenrbnsn Posted July 26, 2006 Share Posted July 26, 2006 Where did the number in the $date variable come from?Ken Link to comment https://forums.phpfreaks.com/topic/15652-doing-the-math/#findComment-63805 Share on other sites More sharing options...
uknowho008 Posted July 26, 2006 Author Share Posted July 26, 2006 my database Link to comment https://forums.phpfreaks.com/topic/15652-doing-the-math/#findComment-63806 Share on other sites More sharing options...
kenrbnsn Posted July 26, 2006 Share Posted July 26, 2006 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 More sharing options...
uknowho008 Posted July 26, 2006 Author Share Posted July 26, 2006 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 More sharing options...
kenrbnsn Posted July 26, 2006 Share Posted July 26, 2006 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 More sharing options...
uknowho008 Posted July 26, 2006 Author Share Posted July 26, 2006 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 More sharing options...
448191 Posted July 26, 2006 Share Posted July 26, 2006 Use mysql's own date and time functions!On select, always use UNIX_TIMESTAMP, for easy use in php.SELECT UNIX_TIMESTAMP(`somedatetimefield`) AS somedatetimefieldto 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.htmlConverting 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.