Vivid Lust Posted October 30, 2009 Share Posted October 30, 2009 How could I use a timestamp (e.g 2009-10-30 13:20:35 ), and with php find out if it is: from today from this week from this month from the last 3 months from the last 6 months from this year from 1 year ago etc etc ? Thanks Link to comment https://forums.phpfreaks.com/topic/179630-mysql-timestamp-manipulation/ Share on other sites More sharing options...
smerny Posted October 30, 2009 Share Posted October 30, 2009 good solution found here: http://www.phpbuilder.com/board/showthread.php?t=10100466 ## example start/end dates $startdate = ( time() - 3587 ); $enddate = time(); ## difference between the two in seconds $time_period = ( $enddate - $startdate ); $days = 0; $hours = 0; $minutes = 0; $seconds = 0; $time_increments = array( 'Days' => 86400, 'Hours' => 3600, 'Minutes' => 60, 'Seconds' => 1 ); ## will hold our values for ( day, minute, hour, seconds ) $time_span = array(); ## cycle through time_increments while( list( $key, $value ) = each( $time_increments )) { $this_value = (int) ( $time_period / $value ); $time_period = ( $time_period % $value ); # save value $time_span[$key] = $this_value; } ## show results while( list( $key, $value ) = each( $time_span )) { print "$key $value<br>\n"; } Link to comment https://forums.phpfreaks.com/topic/179630-mysql-timestamp-manipulation/#findComment-947821 Share on other sites More sharing options...
Vivid Lust Posted October 30, 2009 Author Share Posted October 30, 2009 One last question, How would I check if "2009-10-30 13:20:35" is older than a week? Thanks. Link to comment https://forums.phpfreaks.com/topic/179630-mysql-timestamp-manipulation/#findComment-947828 Share on other sites More sharing options...
Mchl Posted October 30, 2009 Share Posted October 30, 2009 SELECT "2009-10-30 13:20:35" < DATE_ADD(NOW(), INTERVAL -7 DAY) Link to comment https://forums.phpfreaks.com/topic/179630-mysql-timestamp-manipulation/#findComment-947840 Share on other sites More sharing options...
smerny Posted October 30, 2009 Share Posted October 30, 2009 With PHP? Check if the difference (subtraction) between the two dates is greater than 7 * 86400 Link to comment https://forums.phpfreaks.com/topic/179630-mysql-timestamp-manipulation/#findComment-947847 Share on other sites More sharing options...
Vivid Lust Posted October 30, 2009 Author Share Posted October 30, 2009 Any how to check if its from today? Link to comment https://forums.phpfreaks.com/topic/179630-mysql-timestamp-manipulation/#findComment-947864 Share on other sites More sharing options...
Mchl Posted October 30, 2009 Share Posted October 30, 2009 SELECT DATE("2009-10-30 13:20:35") = CURDATE() Link to comment https://forums.phpfreaks.com/topic/179630-mysql-timestamp-manipulation/#findComment-947868 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.