Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In a previous chapter of the video series, I described how to use the Zend framework, how to install and configure it in your PHP installation so that you could take advantage of all of its valuable validation, formatting, and other tools. I'm now going to show you how to use the Zend framework's zendDate class to format values that you retrieve from your MySQL Server. I'll start in Code View. I'm working in the file explorerlist2.php, in the explorers' folder of the 05_bindingandbehaviors folder, which is currently set as my site foot.
I'll start at the top of the page. Before I use any class from the Zend framework, I first have to include the class definition. So I'll place the cursor at the end of the first PHP code block, and then I'll press Enter to create a new line. I'll go to the Insert panel, to the PHP category, and I'll insert a new PHP code block. Within the code block, I'll use the require_once command. I'll type in r-e-q, and press Ctrl+Space, and I'll select the require_once function.
Then I'll pass in the literal string pointing to the name of the file, relative to the Zend framework library folder, which is already included in my PHP installation. The relative location of the PHP class definition is Zend/Date.php. Now, I'm ready to use that class to format my dates. I'll first locate the data that I want to format. I'll go to Design View. Then in the dynamic table, I'll click on the dob column.
Then I'll go back to Code View. That takes me to the value I am interested in formatting. I'm going to take the value that I'm currently outputting, the dob column of the row rsExplorers variable, I'll select that value - not including the semicolon - and then I'll cut it to the clipboard. Now, I'll place the cursor after the PHP code block that's creating a loop over the data. I'll create a new line after that code block, then I'll go to the Insert panel and click once on Code Block to insert the code block.
Within the new code block, I am going to create a new instance of the Zend/Date class. It will look like this: zendDate = new, and then I'll type in Zend_, and using code hinting, I'll choose the Zend_Date class. I'll create an opening parenthesis, and then for the first value that's passed in, I'll paste, and that pastes in the dob expression that I cut from the original dynamic table.
I'll type in a closing parenthesis and a semicolon, and then clean up the code a little bit to make it more readable. Now, I'm going to move down to the line that currently shows a syntax error. This is where I removed the value that I wanted to echo, or send to the browser. And I'm going to replace it with an expression that outputs the date object using my selected formatting. It will look like this. I'll start with a dollar sign, then zendd. That takes me to my variable zendDate.
I'll press Enter to select it from the code hinting list. Then I'll type in a dash, greater than, and I'll select the toString function. Make sure you choose the toString function without the underscore prefixes. I'll choose the function, and then for the format, I'll pass in a string that looks like this: an uppercase M, a slash, a lowercase d, another slash, and two lowercase yys. I'll type into closing quote and the closing parenthesis.
And now Dreamweaver shows that there are no syntax errors. So each time through the loop, I'm creating a new instance of the zendData class, passing in the value from the database, and then within the loop, to output, I'm using the toString function of the zendDate object and passing in my preferred formatting string. Once again, you can look at the documentation for the zendDate class, to find your own preferred formatting strings. I'll save and go to Live View, update the file on the testing server, look at it in Design View, and there are my formatted dates.
You can format the dates any way you like, using the date format values that the zendDate class supports. So in this example, I've shown you how to use a bit of hand coding and used the Zend framework's classes to do the formatting, and combined that with the code that was generated by Dreamweaver as a dynamic HTML table. You can now format the table as much as you like, using whatever Cascading Style Sheet or other formatting tools you know how to use.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 91242 Viewers
80 Video lessons · 138207 Viewers
59 Video lessons · 57036 Viewers
52 Video lessons · 70676 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.