jarvis Posted July 26, 2010 Share Posted July 26, 2010 Hi All, I'm trying to work out 2 date functions but am having trouble, hope fully, someone can help me out: 1) To work out the next month and how many days there are. need it to be able to say July | 1st August - 31st August 2) To do a 'this week' option. Starting the week on a Monday. So I can say today is Wednesday (for example) W/c Monday 26 July - 1st August Any help is much appreciated! Thanks in advanced Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/ Share on other sites More sharing options...
aleX_hill Posted July 26, 2010 Share Posted July 26, 2010 Take a look at this: <?php $thisMonth = date("m"); //gets the 2 digit expression of the current month if($thisMonth == 12) //If we are in Dec { $nextMonth = "01"; //Make the next month Jan } else { $nextMonth = $thismonth + 1; //Otherwise add one the the current month } $daysInNextMonth = date("t",mktime(0,0,0,$nextmonth,1,date("Y"))); //set the time to the 1st of the next month of the current year (as the only time that the number of days change is in Feb, using current year is OK echo $daysInNextMonth; ?> All you need to know can be found at http://php.net/manual/en/function.date.php As for part two, I dont really understand what you are looking for. Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091252 Share on other sites More sharing options...
jarvis Posted July 26, 2010 Author Share Posted July 26, 2010 Thanks aleX_hill but how do I get the 1st day of the month? Also, I'm still battling with how to get the start and end of a week no matter which day of the week you're on. many thanks Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091260 Share on other sites More sharing options...
aleX_hill Posted July 26, 2010 Share Posted July 26, 2010 date("t",mktime(0,0,0,$nextmonth,1,date("Y"))); will give you the full name of the day for the first of the next month (ie "Sunday") I assume that you want to know which day (number) of the month the beginning and end of the week are, ie the sunday is on the 10th and the saturday is on the 16th: <?php $dayOfWeek = date("w"); //The number of the day of the week. Sun = 0, Sat = 1 $todayDayOfMonth = date("d"); //The day of the month $thisMonth = date("m"); //The number of this month. Jan = 1, Dec = 12 $firstDayOfWeek = $todayDayOfMonth - $dayOfWeek; //The day of the month for the beginning of the week (Sun) if($firstDayOfWeek < 1) //The beginning of the week was in the previous month { $monthBeginWeek = $thisMonth - 1; $firstDayOfWeek = $firstDayOfWeek + date("t",mktime(0,0,0,$monthBeginWeek,1,date("Y"))); } else { $monthBeginWeek = $thisMonth; } $lastDayOfWeek = $todayDayOfMonth + (6 - $dayOfWeek); //Get the day of the month of the end of the week (sat) if($lastDayOfWeek > date("t")) //if the day of the month is larger then the number of days in the month { $monthEndWeek = $thisMonth + 1; $lastDayOfWeek = $lastDayOfWeek - date("t"); //Then take the number of days in the week to get the day number of the next week } else { $monthEndWeek = $thisMonth; } echo "The week begins on ".date("l d F",mktime(0,0,0,$monthBeginWeek,$firstDayOfWeek,date("Y"))); echo "<br>"; echo "The week ends on ".date("l d F",mktime(0,0,0,$monthEndWeek,$lastDayOfWeek,date("Y"))); ?> There will be a problem if the beginning and end of the week span over the Dec 31/ Jan 1 range as I use date("Y") when I make the date in the last two lines. You can challenge yourself by using an if to check whether when you add on to $monthEndWeek is is over 12 (ie into the next year) or if $monthBeginWeek is < 1 (going into the previous year) and then set a $year variable to use it the display lines Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091271 Share on other sites More sharing options...
jarvis Posted July 26, 2010 Author Share Posted July 26, 2010 Thanks aleX_hill I think that answers my question. I'll go play with the code & see what I can do. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091333 Share on other sites More sharing options...
jarvis Posted July 26, 2010 Author Share Posted July 26, 2010 Duplicate posting as page timed out Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091335 Share on other sites More sharing options...
jarvis Posted July 26, 2010 Author Share Posted July 26, 2010 Hi Sorry, I'm having trouble with the code: $thisMonth = date("m"); //gets the 2 digit expression of the current month if($thisMonth == 12) //If we are in Dec { $nextMonth = "01"; //Make the next month Jan } else { $nextMonth = $thismonth + 1; //Otherwise add one the the current month } $daysInNextMonth = date("t",mktime(0,0,0,$nextmonth,1,date("Y"))); //set the time to the 1st of the next month of the current year (as the only time that the number of days change is in Feb, using current year is OK echo $daysInNextMonth; The above alwyas returns 31 days in the next month, even if you're in August, it says Sept has 31. Have I done something wrong? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091337 Share on other sites More sharing options...
aleX_hill Posted July 26, 2010 Share Posted July 26, 2010 The next month is August, which has 31 days. If you want to test it out, try doing this as the first line: $thisMonth = date("m",mktime(0,0,0,NUMBER OF MONTH HERE,1,YEAR HERE); For example, if we pretend we are now in August, and want to know how many days in September, try this: $thisMonth = date("m",mktime(0,0,0,8,1,2010)); EDIT: Sorry, I had a few missing capital letters for variables, try this: <?php $thisMonth = date("m",mktime(0,0,0,8,1,2010)); //gets the 2 digit expression of the current month if($thisMonth == 12) //If we are in Dec { $nextMonth = "01"; //Make the next month Jan } else { $nextMonth = $thisMonth + 1; //Otherwise add one the the current month } $daysInNextMonth = date("t",mktime(0,0,0,$nextMonth,1,date("Y"))); //set the time to the 1st of the next month of the current year (as the only time that the number of days change is in Feb, using current year is OK echo $daysInNextMonth; ?> Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091340 Share on other sites More sharing options...
jarvis Posted July 27, 2010 Author Share Posted July 27, 2010 Thanks again aleX_hill but I still have an issue with the days in the month! If I alter my PC clock it says the next month is August and 30 days - which is wrong as should be 31. Equally, if I alter my PC clock to August, the next month is then September and this is then correct. It's like it's stuck on 30 days now the code has been changed. Sorry to be a pain! Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091578 Share on other sites More sharing options...
RussellReal Posted July 27, 2010 Share Posted July 27, 2010 Take a look at this: <?php $thisMonth = date("m"); //gets the 2 digit expression of the current month if($thisMonth == 12) //If we are in Dec { $nextMonth = "01"; //Make the next month Jan } else { $nextMonth = $thismonth + 1; //Otherwise add one the the current month } $daysInNextMonth = date("t",mktime(0,0,0,$nextmonth,1,date("Y"))); //set the time to the 1st of the next month of the current year (as the only time that the number of days change is in Feb, using current year is OK echo $daysInNextMonth; ?> All you need to know can be found at http://php.net/manual/en/function.date.php As for part two, I dont really understand what you are looking for. mktime for months if you specify 13 it will go back to january The next month is August, which has 31 days. If you want to test it out, try doing this as the first line: $thisMonth = date("m",mktime(0,0,0,NUMBER OF MONTH HERE,1,YEAR HERE); For example, if we pretend we are now in August, and want to know how many days in September, try this: $thisMonth = date("m",mktime(0,0,0,8,1,2010)); EDIT: Sorry, I had a few missing capital letters for variables, try this: <?php $thisMonth = date("m",mktime(0,0,0,8,1,2010)); //gets the 2 digit expression of the current month if($thisMonth == 12) //If we are in Dec { $nextMonth = "01"; //Make the next month Jan } else { $nextMonth = $thisMonth + 1; //Otherwise add one the the current month } $daysInNextMonth = date("t",mktime(0,0,0,$nextMonth,1,date("Y"))); //set the time to the 1st of the next month of the current year (as the only time that the number of days change is in Feb, using current year is OK echo $daysInNextMonth; ?> for your second quote you'd want to use the date format character 't' not 'm' t will return the number of days in the current month Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091580 Share on other sites More sharing options...
aleX_hill Posted July 27, 2010 Share Posted July 27, 2010 Thanks again aleX_hill but I still have an issue with the days in the month! If I alter my PC clock it says the next month is August and 30 days - which is wrong as should be 31. Equally, if I alter my PC clock to August, the next month is then September and this is then correct. It's like it's stuck on 30 days now the code has been changed. Sorry to be a pain! Is your webserver on your local computer? If you are uploading to a third party server, the time and date functions are relative to the server time, so changing your computer clock will have no effect. If the server is on your local computer then I am not sure, I dont have much experience with local servers and time functions. Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091587 Share on other sites More sharing options...
RussellReal Posted July 27, 2010 Share Posted July 27, 2010 The next month is August, which has 31 days. If you want to test it out, try doing this as the first line: $thisMonth = date("m",mktime(0,0,0,NUMBER OF MONTH HERE,1,YEAR HERE); For example, if we pretend we are now in August, and want to know how many days in September, try this: $thisMonth = date("m",mktime(0,0,0,8,1,2010)); EDIT: Sorry, I had a few missing capital letters for variables, try this: <?php $thisMonth = date("m",mktime(0,0,0,8,1,2010)); //gets the 2 digit expression of the current month if($thisMonth == 12) //If we are in Dec { $nextMonth = "01"; //Make the next month Jan } else { $nextMonth = $thisMonth + 1; //Otherwise add one the the current month } $daysInNextMonth = date("t",mktime(0,0,0,$nextMonth,1,date("Y"))); //set the time to the 1st of the next month of the current year (as the only time that the number of days change is in Feb, using current year is OK echo $daysInNextMonth; ?> ALSO (and sorry for the double post) if you are in month december.. when it turns to january for $nextMonth which would be 1 you would be going from december 2010 to january 2010 instead of december 2010 to january 2011 which could tamper with the days of the week.. for example 1 year the first of january will be a monday, the next year it could be a wednesday or whatever, you're better off letting it go to 13 and it will push the year up aswell Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091589 Share on other sites More sharing options...
aleX_hill Posted July 27, 2010 Share Posted July 27, 2010 RussellReal: I did realise that the days would be wrong if the week spanned across the year break (see my comment at the end of reply #3) and coincidently it was when I was working on another third party script that I learn about the mktime and having month set to 13 etc just this morning that I learnt that could happen. Just goes to show that even the "semi-experienced" of us still have a lot to learn Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091590 Share on other sites More sharing options...
RussellReal Posted July 27, 2010 Share Posted July 27, 2010 <?php $nextMonth = date("m") + 1; $daysInNextMonth = date("t",mktime(0,0,0,$nextMonth)); echo $daysInNextMonth; ?> this should do, I wasn't knocking you alex, people are lucky to have you here I have been away from this site for a couple months now, the more coders helping people the more the community grows Sorry if I sounded offensive I was just tryna help out Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091591 Share on other sites More sharing options...
jarvis Posted July 27, 2010 Author Share Posted July 27, 2010 Hi, Thanks for the replies. I'm developing & testing locally but using a live server. Anything date wise I test locally as it means I can check code works by altering my PC clock. Will try the above, thanks once again, it's very much appreciated!!! Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091592 Share on other sites More sharing options...
aleX_hill Posted July 27, 2010 Share Posted July 27, 2010 Not offensive at all. I only really come here when I need help, and try and help others while I wait for replies, then i stick around for a few days before I forget to keep checking back. I know its bad, but better then nothing I guess Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091595 Share on other sites More sharing options...
jarvis Posted July 27, 2010 Author Share Posted July 27, 2010 Hi Guys, RussellReal, your code seems to get around the 30 days issue. One quick question. you need to use the date("m") within the code, which means if you echo out $nextMonth = date("m") + 1; echo $nextMonth; It says 8 rather than August. How do I then convert the 8 into August? If I change the "m" to "F" it stops the code working. Sorry to sound such a noob! Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091637 Share on other sites More sharing options...
aleX_hill Posted July 27, 2010 Share Posted July 27, 2010 Try this: $nextMonth = date("m") + 1; echo date("F",mktime(0,0,0,$nextMonth)); It seems that you understand how the date() function works (ie you need to put in F to get the month name) now take a look at the mktime() function and it should all make sense Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091649 Share on other sites More sharing options...
PFMaBiSmAd Posted July 27, 2010 Share Posted July 27, 2010 As I posted in your other thread you started for this same problem (why did you do that), if you perform the date math inside of the mktime() function it will rollover the months/years correctly. You won't even need to use the if(){}else{} code to test when the month needs to be reset back to 01. Quote Link to comment https://forums.phpfreaks.com/topic/208916-php-date-functions/#findComment-1091667 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.