Subtracting day from date

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ziycon
    Contributor
    • Sep 2008
    • 384

    #1

    Subtracting day from date

    I have a script that outputs 'Today' or 'Yesterday' for the date a piece of content was posted but if its the first of the month yesterday don't work.

    Is there a way to subtract one day from the date variable, the below code I'm using at the moment is:
    Code:
    if(date("dmY") == date("dmY", strtotime($datetimeIn)))
        $datetime = 'Today @ '.date("H:i", strtotime($datetimeIn));
    else if((date("d")-1).date("mY") == date("dmY", strtotime($datetimeIn)))
        $datetime = 'Yesterday @ '.date("H:i", strtotime($datetimeIn));
    else
        $datetime = date("D jS, F Y @ H:i", strtotime($datetimeIn));
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    Is there a way to subtract one day from the date variable
    Well you don't have a $date variable,
    but as you are using unix timestamp this is -86400

    Comment

    • ziycon
      Contributor
      • Sep 2008
      • 384

      #3
      If I did it like this would it make it easier to subtract a date?
      Code:
      $date = strtotime($datetimeIn);
      
      if(date("dmY") == date("dmY", $date))
           $datetime = 'Today @ '.date("H:i", $date);
      else if((date("d")-1).date("mY") == date("dmY", $date))
           $datetime = 'Yesterday @ '.date("H:i", $date);
      else
           $datetime = date("D jS, F Y @ H:i", $date);

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        In your code $datetime is a string.
        $date is a timestamp.
        $date - 86400 is one day less
        But if you are comparing dates using strings, strtotime is very clever, it even understands things like 'yesterday' and '-1 day'.

        Comment

        • ziycon
          Contributor
          • Sep 2008
          • 384

          #5
          I get you now, I'm just a bit confused as to how I'm going wrong implementing it.

          I want the current date to be subtracted by one day, so I'm using the below code but I know I'm doing it wrong but not sure what I'm missing?
          Code:
          else if(date("dmY")-86400 == date("dmY", $date))
          or should it be:
          Code:
          else if(strtotime(date("dmY"))-86400 == date("dmY", $date))

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            Code:
            else if(date("dmY")-86400
            You cannot subtract 86400 off the return from date() because date() returns a string
            Code:
            else if(strtotime(date("dmY"))-86400 == date("dmY", $date))
            And here you are comparing a string to timestamp which is an integer.
            If you tried doing that in C++ you would be arrested.
            Try
            Code:
            if(date('Ymd',strtotime('yesterday')) == date("dmY", $date)))
            Or
            Code:
            if(date('Ymd',time()-86400)) == date("dmY", $date)))
            Might need a little tweaking

            Comment

            • ziycon
              Contributor
              • Sep 2008
              • 384

              #7
              Appreciate your help, the two solution you gave worked, I went with the second one. Thanks again for your help.

              Comment

              Working...