PDF='MAIN' { ATTRIBUTES='*' { PLATFORM='php.txt'; REPTYPE='1'; ATTACHED='0'; PROJECT='36'; OBJECTNAME='date.php'; LIBLOBJ='*FILES'; TITLE='Example 144: Converting a Date from User Input'; WEBPATH='http://esdi.excelsystems.com/wsexmp/'; TARGETPATH='/www/zendcore/htdocs/examples/'; FTPSITE='examples'; SET='46'; VERSION='4.00'; CRTBNUM='Revision 1811'; CRTTEMPLT='C:\\Program Files\\ESDI\\WebSmart\\Temp\\PHP-A Simple Page.tpl'; SAVBNUM='Revision 1811'; LASTLOC='1'; VHTML='N'; LASTSAVE='12/24/2007 08:25:11'; OBJLIBRARY='XL_WEBEXMP'; SOURCELIBR='XL_WEBEXMP'; SOURCEFILE='QRPGLESRC'; } FTPREFINFO='*' { URL='192.168.0.100'; INITIALDIR='/www/zendcore/htdocs/examples/'; FTPPORT='21'; PASSIVEMODE='1'; WEBPATH='http://esdi.excelsystems.com/webtest/'; } PANELS='*' { PANEL='MainSeg' { DESC='Main Segment'; DETAILS=' WS Example: Converting a Date from User Input

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
$to_date
  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.

'; } } PREVIEWS='*' { DEFAULT='MainSeg'; PREVIEW='MainSeg' { SEG='MainSeg'; } } ACTIONS=' // Program Name: date.php // Program Title: Example 145: Converting a Date from User Input // Created by: TMC // Template family: Idaho // Template name: Simple Page // Purpose: // Program Modifications: require(\'/esdi/websmart/v6.6/include/xl_functions001.php\'); global $wsnum; // Add this code to the \'Simple Page Template\' global $from_date, $to_date; if (isset($_REQUEST[\'date\'])) { $from_date = $_REQUEST[\'date\']; } // This code is for the Rate this page section if (isset($_REQUEST[\'wsnum\'])) { $wsnum = $_REQUEST[\'wsnum\']; } // Check the cookie if(isset($_COOKIE[\'wsnumbc\'])) { $wsnum = $_COOKIE[\'wsnumbc\']; } else { setcookie(\'wsnumbc\', $wsnum); } // Retrieve the task (default to "default") if ($pf_task == \'default\') generic(); if ($pf_task == \'convert_date\') convert_date(); function generic() { wrtseg(\'MainSeg\'); } function convert_date() { // Make all global variables available here foreach($GLOBALS as $arraykey=>$arrayvalue) { global $$arraykey; } // convert a string to a timestamp and, then, from a timestamp // to a string of a different format if ( strtotime($from_date) ){ $to_date = strftime("%Y/%m/%d", strtotime($from_date)); $to_date = "The date is ".$to_date; } else{ $to_date = "You did not enter a valid date format"; } wrtseg(\'MainSeg\'); }'; }