Originally posted by searain
if user put in a date such as 6/31/2002, i want the php page show it as 7/1/2002. how? i think use date() and maketime() functions can do it, but i didn't figure it out how.

thanks

Well, get the bits out:

list($month,$day,$year)=sscanf("%d/%d/%d",$date);

put them into a timestamp:

$date=mktime(0,0,0,$month,$day,$year);

and get them out again

$date=date('n/j/Y',$date);

    here's my quick and dirty version of it:

    <?php
    
    $date = "06/31/2002";
    
    if(strftime("%M/%d/%Y", strtotime($date)) != $date) {
    	list($month, $day, $year) = explode("/", $date);
    	if($month < 12) {
    		$month++;
    	}
    	else {
    		$month = 1;
    		$year++;
    	}
    	$day = 1;
    
    $newdate = $month . "/" . $day . "/" . $year;
    echo $newdate;
    }
    
    ?>
    

    perhaps someone has a simpler/more compact solution?

    -sridhar

      First make sure that the date you've inputted is in a date form and not a string by using the strtotime function. So....

      $DateValue = "6/31/02";
      $newDateValue = strtotime($DateValue);

      After that use the mktime function to convert this to a timestamp. So it'd probably look something like this...

      $var = mktime(0, 0, 0, date("m", $newDateValue), date("d", $newDateValue), date("Y", $newDateValue));

      The first 3 zeros are the for hour, minute and second in that order. I'm guessing you don't just want the date in there and not the time so those are set to zero. The mktime function should convert it to July 1. When you output the variable make sure you format it from a timestamp to a regular date

      $newVar = date("m/d/Y", $var);
      print("$newVar");

      or something like that.

      For more info...

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

      Hope this helps.

        Originally posted by chairmanhong
        $DateValue = "6/31/02";
        $newDateValue = strtotime($DateValue);

        Problem, is, is 8/10/2002 in August or October? And how is strtotime() to know?

          I think it will know by what locale is set. If you set UK (or other countries that use the DD/MM format), then it will go with Oct. Otherwise with August

          -sridhar

            Originally posted by theslinky
            I think it will know by what locale is set. If you set UK (or other countries that use the DD/MM format), then it will go with Oct. Otherwise with August
            -sridhar

            ...or pretty much any country except the United States. Why can't people use sensible formats, like YYYY-MM-DD?

            Thing about strtotime() is, the user could just enter 'last Tuesday' and it will be accepted. If you actually want to enforce the mm/dd/yyyy format:

            list($month,$day,$year)=sscanf("%d/%d/%d",$date);
            if(isset($month) && isset($day) && isset($year) && $year>999)
            {   $date=mktime(0,0,0,$month,$day,$year);
                $date=date('n/j/Y',$date);
            }
            else
            {   /* $date is bogus */
            

            This won't get upset if you try for something like 15/144/1977 - it will assume you meant 7/22/1978.

              Well, my sloppy lazy version would be...

              $new_date = str_replace('06/31/2002','07/01/2002',$date);
              

              Hahah. 😃

                if user put in a date such as 06/31/2002, i want the php page show it as 07/01/2002. how? i think use date() and maketime() functions can do it, but i didn't figure it out the detail yet.

                thanks

                $parts = explode('/',$date);
                $month = $parts[0];
                $day = $parts[1];
                $year = $parts[2];
                if($month == '12' && $day == '31')
                {
                $year++;
                }
                if($day == '31')
                {
                $month++;
                $day = '01';
                }
                $date = $month . '/' . $day . '/' . $year;
                echo $date;
                

                  Ok, the real quick version is...

                  <?
                  function gDate($date)
                  {
                          $parts = explode('/',$date);
                          $month = $parts[0];
                          $day = $parts[1];
                          $year = $parts[2];
                          if($month >= '12' && $day >= '31')
                          {
                                  $year++;
                          }
                          if($day >= '31')
                          {
                                  $month++;
                                  $day = '01';
                          }
                          if(substr($month,0,1) != '0') $month = '0' . $month;
                          $date = $month . '/' . $day . '/' . $year;
                          echo $date;
                  }
                  gDate('06/31/2002');
                  ?>
                  

                  This outputs:
                  07/01/2002

                    Originally posted by intenz
                    Ok, the real quick version is...

                    <?php
                    gDate('06/31/2002');
                    ?>
                    

                    This outputs:
                    07/01/2002 [/B]

                    And what does gDate('12/31/2002') output?

                    Really, what's wrong with my code, then?

                      Well since 6/31/2002 is an invalid date, this should do the trick:

                      function fix_date($date)
                      {
                      	$pieces = explode("/", $date);
                      	return date("m/d/Y", mktime(0,0,0,$pieces[0],$pieces[1],$pieces[2]));
                      }
                      
                      echo fix_date("06/31/2002");

                      This'll output 07/01/2002.

                      I'll assume you just wanted to fix invalid dates and not increment every single date 🙂 Hope this helps.

                        Originally posted by Josh
                        Well since 6/31/2002 is an invalid date, this should do the trick:

                        function fix_date($date)
                        

                        This'll output 07/01/2002.

                        I'll assume you just wanted to fix invalid dates and not increment every single date 🙂 Hope this helps. [/B]

                        Virtually identical to what I wrote more'n twelve hours ago.

                          Write a Reply...