Formatting Output Data / Formatting Dates & Times
Visitors to VTC.com will be able to view all introductory videos for each training course.
Free Trial Members will gain access to first three chapters for each training course.
Full Access Members have full access to VTC.com’s entire library of video tutorials.
Learn More
Subtitles of the Movie
In this lesson we're going to talk about formatting dates and time stamps in PHP. Essentially we want to take a date that's in FileMaker and then we want to display that on the web and see what our options are for formatting those dates. And while we're doing that, we'll talk about Unix time stamps and what they do for us. First of all, let's select a date that we're going to use inside of our web page. When we open up our asset management database, you'll see that we have a date here called in-service. If you go to layout mode, we can see that that date is actually defined to be called date placed. So what we did is in on our web item detail layout, we placed the field date placed. Then what we did is in our asset detail page we added that to our list of fields that we can edit. Now, if we go to our 0105 folder and we select one of our asset items, you can see that we have the in-service date and we're pulling the date placed. Now, let's talk about what we can do with this date. By default, FileMaker's API gives us this date in this simple format. It doesn't matter how we change that format on our layout, such as going into date format and specifying another format. Even though in the layout it shows the changed format, it doesn't show that for us on the web. When we hit refresh, the date still comes to us in this simple month day year format. So how do we format these dates with PHP because we're going to want to change the date format from page to page. Well, what we do is we work with what's called a Unix time stamp. The Unix time stamp is basically the number of seconds since January 1, 1970. So the Unix time stamp for this would be a certain amount of seconds that this date of midnight occurred since January 1, 1970. Once we have the Unix time stamp, then we can use a PHP function to format it called the date function. First of all, let's go to our code here and let's go into PHP mode and let's say that our time stamp is going to be equal to, and we need the time stamp for this particular field. One of the ways that we can get that time stamp is with a function called string to time. Here we could place this field and then we could echo out our time stamp and refresh this in the browser and here you can see our Unix time stamp. Sometimes the string to time function, though, won't always work because if you're time or the string that you're passing isn't a recognizable string, it won't be able to convert it. Because of this and to keep things simple, FileMaker has included a special function in the API for the record object called get Unix time stamp. Let's look at our API documentation. If we look at the record object, you can see that we have a method called get field as time stamp. If the field is a date field, then the time stamp is going to be for the field date at midnight. If the field's a time field, the stamp is going to be for that time on January 1, 1970. If it's a time field, it doesn't matter that it's on this particular date because you're just going to be using the time attribute. So we could just use the get as time stamp method instead of the get field. Get us time stamp date place and then we don't need the string to time function. When we refresh, here we can see we still have the Unix time stamp. Once you have a Unix time stamp, this is really what you're after. Then you can use the PHP date function. The PHP date function allows you to pass a Unix time stamp as it's second parameter and if you don't pass it, then it's going to grab the current date, the local date and time and it's going to allow you to pass a string, which is your formatting string. In that string you're going to specify any one of these format characters. For instance, if we wanted the month day year format, we would pass the M, D and Y. If I go to our code here and I say let's echo out, or let's say that our date is equal to the function date and then we specify a string month and then any character you pass that's not one of the recognizable characters will just get outputted as you type it. So month, day and year and then our time stamp and then we echo out the date field inside of our value. When we refresh this, we can see our new date format, which didn't get outputted because we need to echo. You can see our month, day, year format. By default, FileMaker was giving us a four-year digit and now we have the two-year digit. So we just need to learn how to use these different characters to get the date or the time format that we want. That's another reason why I use one of these cheat sheets in my dashboard because it's easy for me to select all my different date formatting options. Twelve-hour leading zeros or just 12-hour minutes, seconds, date of year and so forth. So remember, you want to work with the Unix time stamp and to get that you can use the FileMaker record function called get field as time stamp. Then once you have a time stamp, you can simply use the date function to format it how you want. You can also use time stamps when you're adding and subtracting dates in calculations because it ends up being an integer. This concludes our lesson on formatting dates and times using FileMaker's API and PHP.
Tutorial Information
| Course: | FileMaker 9 & PHP Foundations |
| Author: | Lance Hallberg |
| SKU: | 33786 |
| ISBN: | 1-933736-99-2 |
| Release Date: | 2007-08-22 |
| Duration: | 9.5 hrs / 107 lessons |
| Work Files: |
Yes |
| Captions: | For Online University members only |
| Compatibility: |
Vista/XP/2000, OS X, Linux QuickTime 7, Flash 8 |
VTC Sign up & Benefits
- Unlimited Access
- 98,729 Video Tutorials (23,265 free)
- Video Available as Flash or QuickTime
- Over 1026 Courses
- $30 for One Month Access
- Multi-User Discounts Available
United States 