Php date problem 'Y' displying 1969

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Publisher Bucks
    Confirmed User
    • Oct 2018
    • 1395

    #1

    Tech Php date problem 'Y' displying 1969

    Why is this code displaying 'Y' as 1969?

    <?php

    $today = date("d");
    $thisMonth = date("M");
    $thisYear = date("Y");
    echo date("M d Y", mktime(0,0,0, $thisMonth, $today, $today-8));

    ?>
    I'm trying to get it to display: Jan 5 2023 (minus 8 days from today).
  • redwhiteandblue
    Bollocks
    • Jun 2007
    • 2785

    #2
    Because you have given the mktime() function a negative number for the year value.
    Interserver unmanaged AMD Ryzen servers from $73.00

    Comment

    • Publisher Bucks
      Confirmed User
      • Oct 2018
      • 1395

      #3
      Originally posted by redwhiteandblue
      Because you have given the mktime() function a negative number for the year value.
      So I need to take it out of the echo statement and display it within its own echo statement?

      Or just remove the '0' for the year?

      Comment

      • redwhiteandblue
        Bollocks
        • Jun 2007
        • 2785

        #4
        No, you need to understand what mktime() does and what parameters it expects.

        https://www.php.net/manual/en/function.mktime.php

        You are giving it a value for the day where it expects a value for the year.
        Interserver unmanaged AMD Ryzen servers from $73.00

        Comment

        • redwhiteandblue
          Bollocks
          • Jun 2007
          • 2785

          #5
          It would be far easier to work with timestamps for this.

          Code:
          echo date("M d Y", time() - (86400 * 8));
          Interserver unmanaged AMD Ryzen servers from $73.00

          Comment

          • Publisher Bucks
            Confirmed User
            • Oct 2018
            • 1395

            #6
            Originally posted by redwhiteandblue
            It would be far easier to work with timestamps for this.

            Code:
            echo date("M d Y", time() - (86400 * 8));
            Okay thanks for the suggestion and assistance, I appreciate it

            Comment

            • sarettah
              still insane
              • Oct 2002
              • 14428

              #7
              <?php

              $today = date("d");
              $thisMonth = date("M");
              $thisYear = date("Y");
              echo date("M d Y", mktime(0,0,0, $thisMonth, $today, $today-8));
              ?>
              this is wrong for so many reasons beyond just having your mktime parameters screwed up

              how about when your $today-8 is an invalid day value? on the 7th $day-8 comes back as -1

              also what is with splitting the date into pieces like that? you were doing that in the database table at one point too.

              you are taking your date and converting it to 3 integers and then turning it back into a date, why?

              every language has date functions to handle date calculations, php included

              https://www.w3schools.com/php/php_ref_date.asp

              handling it the way you are doing is basically trying to reinvent the wheel


              echo date("M d Y", time() - (86400 * 8));
              that is a reasonable solution


              i prefer to use datetime objects

              in php you can add and subtract from a date pretty easily using the datetime object and associated functions

              $mydate=date_create(date('Y-m-d',time())); // create the datetime object
              date_sub($mydate, date_interval_create_from_date_string("8 days")); // subtract 8 days
              echo date_format($mydate,"M d Y"); // display the result

              https://www.w3schools.com/php/func_date_date_sub.asp

              you could also use date_modify() for this

              $date=date_create(date('Y-m-d',time()));
              date_modify($date,"-8 days");
              echo date_format($date,"Y-m-d");


              https://www.w3schools.com/php/func_date_modify.asp



              you should, at all times while working on this stuff have a browser window open to php.net and w3schools.com or other coding sites, just my opinion


              .
              all cookies cleared !

              Comment

              • fuzebox
                making it rain
                • Oct 2003
                • 22377

                #8
                sarettah you are definitely a case study in patience

                Comment

                • sarettah
                  still insane
                  • Oct 2002
                  • 14428

                  #9
                  Originally posted by fuzebox
                  sarettah you are definitely a case study in patience
                  I try



                  .
                  all cookies cleared !

                  Comment

                  • k0nr4d
                    Confirmed User
                    • Aug 2006
                    • 9231

                    #10
                    date('M d Y',strtotime("-8 days"));
                    Mechanical Bunny Media
                    Mechbunny Tube Script | Mechbunny Webcam Aggregator Script | Custom Web Development

                    Comment

                    • Mr Pheer
                      Top Rated CEO
                      • Dec 2002
                      • 22160

                      #11
                      The code is displaying 'Y' as 1969 because the third argument passed to the mktime() function is $today-8 instead of $thisYear.

                      Here is the corrected code:
                      Code:
                      <?php
                      
                      $today = date("d");
                      $thisMonth = date("m");
                      $thisYear = date("Y");
                      echo date("M d Y", mktime(0,0,0, $thisMonth, $today-8, $thisYear));
                      
                      ?>
                      This will display the date 8 days ago in the format "Jan 6 2023".

                      Comment

                      • Publisher Bucks
                        Confirmed User
                        • Oct 2018
                        • 1395

                        #12
                        Thank you all for the assistance with this i genuinely appreciate the advice and feedback

                        Comment

                        • Mr Pheer
                          Top Rated CEO
                          • Dec 2002
                          • 22160

                          #13
                          It took maybe 30 seconds to copy the post just as it was written, paste it into ChatGPT, wait for the answer, copy that, come to GFY, and paste it.

                          Learn to use AI or you're getting left behind.

                          Comment

                          Working...