this code :

$timestamp = time();
$date_time_array = getdate($timestamp);
$hours = $date_time_array['hours'];
$minutes = $date_time_array['minutes'];
$seconds = $date_time_array['seconds'];
$month = $date_time_array['mon'];
$day = $date_time_array['mday'];
$year = $date_time_array['year'];
$timestamp = mktime(day -1);
echo strftime('%A %b',$timestamp);
echo ", ";
echo strftime('%d',$timestamp);

echo's this -> "Thursday Apr, 01"

the code below also echo's -^

$timestamp = time();
$date_time_array = getdate($timestamp);
$hours = $date_time_array['hours'];
$minutes = $date_time_array['minutes'];
$seconds = $date_time_array['seconds'];
$month = $date_time_array['mon'];
$day = $date_time_array['mday'];
$year = $date_time_array['year'];
$timestamp = mktime(day -2); <----- ???
echo strftime('%A %b',$timestamp);
echo ", ";
echo strftime('%d',$timestamp);

would anyone mind showing me how to get the last code to work showing 2 days ago in the same format that the first did?

Even if two days ago was the last day of the previous month.
i.e today it should output "Wednsday Mar,31"

I'd appreciate it alot.

😕

    I see two things that might be the problem.

    1. mktime(day -1) should probably be mktime ($day -1), unless there's a "day" constant, or some secret parameter to mktime() that I don't know about 😉

    2. If today is the 2nd and you say "$day -1" you get the 1st. If you say "$day -2" you get 0. Remember, you're only subtracting from the day, which you've defined the day of the month. So $day knows nothing about March or April, it only knows about 1 thru 31.

    Your best bet would be to subtract the required number of days from the original timestamp BEFORE doing your getdate(). So let's see - a day in Unix format is 60 60 24, which is 86400.

    Try this (untested):

    $timestamp = time(); 
    $timestamp = $timestamp - (1 * 86400); // where "1" is the number of days you want to subtract
    
    // Then I think you can drop this whole chunk...
    /*$date_time_array = getdate($timestamp); 
    $hours = $date_time_array['hours']; 
    $minutes = $date_time_array['minutes']; 
    $seconds = $date_time_array['seconds']; 
    $month = $date_time_array['mon']; 
    $day = $date_time_array['mday']; 
    $year = $date_time_array['year']; 
    $timestamp = mktime(day -1); */
    
    echo strftime('%A %b',$timestamp); 
    echo ", "; 
    echo strftime('%d',$timestamp);

    Oh, and the comma should go after the weekday, instead of after the month.

    • Bob

      I will go test it now...

      btw this is code I found elsewhere and
      was trying to use because it seemed to sort of work 🙂

      thank you for your reply 🙂

        Originally posted by budz
        I will go test it now...


        btw this is code I found elsewhere and
        was trying to use because it seemed to sort of work 🙂

        thank you for your reply 🙂

        One suggestion would be to use strtotime() to subtract one day, instead of just subtracting 86400 seconds; not all days are 86400 seconds long!

        $timestamp = strtotime('1 day ago');

        allows for these situations. If you want two days ago the string to use is naturally "2 days ago".

          here is what I have came up with so far..

          I will be working on the month function next.

          any tip/pointers you can give me would also be appreciated.

          thx for the replies so far everyone 😉

          Today: <?php

          $timestamp = time();
          $date_time_array = getdate($timestamp);
          $hours = $date_time_array['hours'];
          $minutes = $date_time_array['minutes'];
          $seconds = $date_time_array['seconds'];
          $month = $date_time_array['mon'];
          $day = $date_time_array['mday'];
          $year = $date_time_array['year'];
          $timestamp = mktime(day);

          $newvar= strftime('%d',$timestamp);
          echo $newvar;

          ?>

          <BR>

          Yesterday:
          <?php

          $yest = ($newvar -1);

          if ($yest == "-2")

          echo "29";

          if ($yest == "-1")

          echo "30";

          if ($yest == "0")

          echo "31";

          else

          if ($yest == "1")

          echo "01";

          else

          if ($yest == "2")

          echo "02";

          else

          if ($yest == "3")

          echo "03";

          else

          if ($yest == "4")

          echo "04";

          else

          if ($yest == "5")

          echo "05";

          else

          if ($yest == "6")

          echo "06";

          else

          if ($yest == "7")

          echo "07";

          else

          if ($yest == "8")

          echo "08";

          else

          if ($yest == "9")

          echo "09";

          else

          echo $yest;

          ?>

          <BR>

          Two Days Ago:

             <?php

          $dayb4 = ($newvar -2);

          if ($dayb4 == "-2")

          echo "29";

          if ($dayb4 == "-1")

          echo "30";

          if ($dayb4 == "0")

          echo "31";

          else

          if ($dayb4 == "1")

          echo "01";

          else

          if ($dayb4 == "2")

          echo "02";

          else

          if ($dayb4 == "3")

          echo "03";

          else

          if ($dayb4 == "4")

          echo "04";

          else

          if ($dayb4 == "5")

          echo "05";

          else

          if ($dayb4 == "6")

          echo "06";

          else

          if ($dayb4 == "7")

          echo "07";

          else

          if ($dayb4 == "8")

          echo "08";

          else

          if ($dayb4 == "9")

          echo "09";

          else

          echo $dayb4;

          ?>

            Originally posted by Weedpacket
            One suggestion would be to use strtotime() to subtract one day, instead of just subtracting 86400 seconds; not all days are 86400 seconds long!

            $timestamp = strtotime('1 day ago');

            allows for these situations. If you want two days ago the string to use is naturally "2 days ago". [/B]

            The man does know his PHP - wow. Must read up on my strtotime()!

            Does Unix actually account for those leap seconds? Crazy! I know they're pretty infrequent. I imagine if you were only using this function to move a few days either way from the current day you wouldn't run into any problems, except perhaps for one second at midnight, on certain days. Still, good to know.

            • Bob

              Budz - there's a tidier way to do your "if ($yest == '1') echo "01"" bit, at least for the postive options:

              sprintf("%02d", $yest);

              This will place leading zeroes before the single digit to make it a 2-digit number. Should save you about 9 conditionals anyway...

              You realize that if the preceding month is not a 31 day month, your results will be off by a day (or 2 or 3 if it's February)?

              What you really need is a nice function you can call. How about this?

              function pastday($daysago) {
                  $timestamp = time() - ($daysago * 86400);
                  $formatted_day = strftime("%A, %b %d",$timestamp);
                  return $formatted_day;
              }
              
              $timestamp = time();
              echo "Today is " . strftime("%A, %b %d",$timestamp) . "<br>";
              
              $yesterday = pastday(1);
              echo "Yesterday was " . $yesterday . "<br>";
              
              $daybefore = pastday(2);
              echo "Day before yesterday was " . $daybefore . "<br>";
              

              Here's the function using Weedpacket's method...

              function pastday($daysago) {
                  $daysago2 = $daysago . " days ago";
                  $timestamp = strtotime("$daysago2");
                  $formatted_day = strftime("%A, %b %d",$timestamp);
                  return $formatted_day;
              }
              

              It might also work to concatenate $daysago and the words " days ago" this way....

              $timestamp = strtotime({$daysago . " days ago"});

              or it might not. I recall running into trouble concatenating within a function call this way, so I stopped doing it. Weedpacket would know...

              • Bob

                Some further reading on relative dates and strtotime include the PHP manual page on strtotime() and the GNU page on relative dates and times.

                Interestingly, the latter page says that while durations like month and year are "fuzzy" (of variable length), other durations are precise, including minute (60 seconds), which would suggest that leap seconds are not actually taken into account.

                • Bob

                  Originally posted by bunner bob
                  Does Unix actually account for those leap seconds?

                  No, but it does account for Daylight Saving in the timezone the server is configured for....

                  (Incidentally, the current status on leap seconds is available [url=ftp://hpiers.obspm.fr/iers/bul/bulc/bulletinc.dat]here[/url])

                    Originally posted by bunner bob
                    It might also work to concatenate $daysago and the words " days ago" this way....

                    $timestamp = strtotime({$daysago . " days ago"});

                    or it might not. I recall running into trouble concatenating within a function call this way, so I stopped doing it. Weedpacket would know...

                    • Bob [/B]

                    It's just

                    $timestamp = strtotime($daysago." days ago");

                    - no {}.

                      Write a Reply...