Example 144: Converting a Date from User Input

Description:   This WebSmart PHP example converts a date input by the user into the format YYYY/MM/DD.

Enter date:
Valid Date Formats:
1972-09-24
72-9-24
72-09-24
9/24/72
24 September 1972
24 Sept 72
24 Sep 72
Sep 24, 1972
24-sep-72
24sep72
  1. Open a new 'Simple Page' template.


  2. Add this code near the top of PHP source-code:
                          global $from_date, $to_date;
                          if (isset($_REQUEST['date']))
                          {
    	                    $from_date = $_REQUEST['date'];	
                          }
    
                          
  3. Add this task to the PHP source-code:
                          if ($pf_task == 'convert_date')
                            convert_date();
                          
  4. Add this function to the PHP source-code:
                          function convert_date()
                          {
                          	// Make all global variables available here
                          	foreach($GLOBALS as $arraykey=>$arrayvalue) 
                          	{
                          		global $$arraykey;
                          	}
                          
                          	if( strtotime($from_date) ){
                          		$to_date = strftime("%Y/%m/%d", strtotime($from_date));
                          	}
                          	else{
                          		$to_date = "Not a valid date format";
                          	}
                          	wrtseg('MainSeg');	
                          }
                          
  5. Add this html to the main segment:
                        
                            <form name="val_date" action="$pf_scriptname" method="POST">
                              <input type="hidden" name="task" value="convert_date">
                              <table>
                                <tr>
                                  <td>Enter date:</td>
                                  <td><input type="text" name="date" value="" size="35"></td>
                                  <td><input type="submit" value="Submit "></td>
                                </tr>
                              </table>
                            </form> 
                            
                            <div style="color:red; margin-left: 100px; text-style:bold;"><b>$to_date</b></div>
                            
                          

Program Definition:   date.phw

Rate This Example

Did this knowledge base article help you to achieve your goal?  Yes  No  Don't Know

Enter additional comments below.   If you want to hear back from us, include your contact information.