Ekate Posted March 30, 2007 Share Posted March 30, 2007 Hello, could you please advice me smth,'cause it works incorrect <?php $curdate=mktime(0,0,0,date("d"),date("m"),date("Y")); echo $curdate; $previousdate=$curdate - 2592000; echo $previousdate; $prdate = date("Y/m/d",$previousdate); echo $prdate; ?> thank you Quote Link to comment https://forums.phpfreaks.com/topic/44923-solved-timestamp-problem/ Share on other sites More sharing options...
hitman6003 Posted March 30, 2007 Share Posted March 30, 2007 Are you trying to get the previous month? If so, just use mktime's ability to subtract... echo "Today: " . date("Y/m/d") . "<br />"; echo "Last month: " . date("Y/m/d", mktime(0, 0, 0, date("m") - 1, date("d"), date("Y"))); Quote Link to comment https://forums.phpfreaks.com/topic/44923-solved-timestamp-problem/#findComment-218147 Share on other sites More sharing options...
obsidian Posted March 30, 2007 Share Posted March 30, 2007 I'm not sure what you're after, but your code looks like it's trying to find the date 30 days prior to the current date. If so, you could use any number of options. Here are a couple to get you started: <?php // Option 1: strtotime() $time = strtotime("today -30 day"); echo date('Y-m-d', $time); // Option 2: time() or mktime() $day = 60 * 60 * 24; $time = time(); // or $time = mktime(); $time -= $day * 30; echo date('Y-m-d', $time); ?> If this isn't what you're after, try telling us what yours is actually showing and what you want it to show. That will help us debug with you. Quote Link to comment https://forums.phpfreaks.com/topic/44923-solved-timestamp-problem/#findComment-218150 Share on other sites More sharing options...
Daniel0 Posted March 30, 2007 Share Posted March 30, 2007 Are you trying to get the previous month? If so, just use mktime's ability to subtract... echo "Today: " . date("Y/m/d") . "<br />"; echo "Last month: " . date("Y/m/d", mktime(0, 0, 0, date("m") - 1, date("d"), date("Y"))); Imagine it was the 1st month (ie January), then the month parameter would have 0 (which is not a valid month). Also, not all months got the same amount of days. Quote Link to comment https://forums.phpfreaks.com/topic/44923-solved-timestamp-problem/#findComment-218155 Share on other sites More sharing options...
Ekate Posted March 30, 2007 Author Share Posted March 30, 2007 Sorry, I really didn't specify what i wanted. You're right,I need to find the date 30 prior to the current. Thank you for your help.With your assistance it works. But I can't understand why my code won't work :-\ Anyway,thank you Quote Link to comment https://forums.phpfreaks.com/topic/44923-solved-timestamp-problem/#findComment-218161 Share on other sites More sharing options...
obsidian Posted March 30, 2007 Share Posted March 30, 2007 But I can't understand why my code won't work :-\ I just realized you have your month and day switched in your mktime() call. That's why it's giving you a date sometime in 2009. Remember, though, with mktime(), it will default to the current date, so you don't have to provide the year, month and day at all: <?php $curdate = mktime(0,0,0,date('m'),date('d'),date('Y')); // notice the switch from yours ?> Quote Link to comment https://forums.phpfreaks.com/topic/44923-solved-timestamp-problem/#findComment-218177 Share on other sites More sharing options...
hitman6003 Posted March 30, 2007 Share Posted March 30, 2007 Imagine it was the 1st month (ie January), then the month parameter would have 0 (which is not a valid month). Also, not all months got the same amount of days. mktime is designed for such an occurrence. Should that happen, it will go to the previous year's December... http://us.php.net/mktime#id3107703 Quote Link to comment https://forums.phpfreaks.com/topic/44923-solved-timestamp-problem/#findComment-218182 Share on other sites More sharing options...
Ekate Posted March 30, 2007 Author Share Posted March 30, 2007 Thank you,obsidian Quote Link to comment https://forums.phpfreaks.com/topic/44923-solved-timestamp-problem/#findComment-218185 Share on other sites More sharing options...
Ekate Posted March 30, 2007 Author Share Posted March 30, 2007 Thank for all of you, so I guess that the problem is solved Quote Link to comment https://forums.phpfreaks.com/topic/44923-solved-timestamp-problem/#findComment-218187 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.