How do you know the following day?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

How do you know the following day?

Post by simonmlewis »

We have a calendar system and need to show all dates in a month.

I know how to make it display a given date from a database - but if I said $firstdate = "2012-07-01", how do I then use a counter to add one to it - to make it shows "2012-07-02".... and add one again... and so on.

I can query at each stage if the date is valid (ie. if it is 31 June 2012, which doesn't exist).

Hope someone can help. I have a feeling it's to do with turning the date into something else first, and then turning it back into a date format..... but that's all.

Thanks in advance.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you know the following day?

Post by Celauran »

simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you know the following day?

Post by simonmlewis »

Thanks.

Here's my problem so far....(or opportunity!!)

Code: Select all

  $postdate = "$year-$month-01";
  $checkmth = $month;
  while($checkmth == $month) 
  {
    $addmth = substr("$postdate", -5, 2);
    $addday = substr("$postdate", -2, 2);
    $addyr = substr("$postdate", -11, 4);
    $h2 = mktime(0, 0, 0, $addmth, $addday, $addyr);
    $d2 = date("F dS, Y", $h) ;
    $w2= date("l", $h) ;
      
    echo "<br/>$w2 ";
    $postdate = strtotime($postdate);
    echo date("d M Y", $postdate);

    $postdate = strtotime(date("Y-m-d", strtotime($postdate)) . " +1 day");
    
    $postdate = date("Y-m-d", $postdate);
echo "$postdate";
    $checkmth = substr("$postdate", -5, 2);
}
This however produces:

[text]Thursday 01 Jun 20121970-01-02[/text]
It won't add one date to the overall date.
If I can get it to go to 2012-06-02..... I'm onto a winner.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you know the following day?

Post by Celauran »

Code: Select all

<?php

$year = '2012';
$month = '06';
$day = '01';

$date = new DateTime("{$year}-{$month}-{$day}");
echo $date->modify('+1 day')->format('Y-m-d');
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you know the following day?

Post by simonmlewis »

Ok I've sorted that bit. Now I have to show times of each day, and if an override is set, only show certain times.

For now, I am showing ALL times, but it won't loop. I don't want to write a full script for each time in the day, so want to loop and at each Counted number, display and query on a time.

Code: Select all

$resultdisable = mysql_query ("SELECT * FROM diaryoverride WHERE dateoverride = '$postdate'")or die(mysql_error());
        $num_overrides = mysql_num_rows($resultdisable);
        if ($num_overrides == 0)
        {
          $count = 1;
          $totalcount = "2";
          if ($count == "1") { $time = "1000";}
          if ($count == "2") { $time = "1230";}
          while ($count != $totalcount)
          {          
          echo "<tr><td nowrap>$w2 ";
          $postdate = strtotime($postdate);
          echo date("d M Y", $postdate);
          echo "</td>";
          echo "<td style='border: 1px solid #000000'>$time $count</td></tr>";
          $count = $count + 1;
          }
        }
This just produces 1000 only. What am I doing wrong? Why won't it then go to 1230?
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How do you know the following day?

Post by Celauran »

Same principle, just use +30 minutes instead of +1 day.
simonmlewis
DevNet Master
Posts: 4435
Joined: Wed Oct 08, 2008 3:39 pm
Location: United Kingdom
Contact:

Re: How do you know the following day?

Post by simonmlewis »

Thanks, tho I cannot do it like that as some of the times are not split perfect. ie, 1000, 1130, 1300, 1400, 1630.....

But I think I have it now. I did it by fluke in the end! lol Added the code, and somehow it worked.
Love PHP. Love CSS. Love learning new tricks too.
All the best from the United Kingdom.
Post Reply