Jump to content

php date functions


jarvis

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 
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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;	
																																	?>

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

<?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 :)

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.