Web Design for BeginnersFree top tips for web designers |
Adding the time or date to a pageBy James Middleton - Added 29th of December 2008In the past, many people have used messy JavaScript to add the current date and/or time to their page. If you are using PHP, then adding and manipulating such timestamps can be incredibly easy. Displaying Time/Date with PHPFirstly, make sure that your page file is save with .php at the end of it and not .htm, .html, .asp, etc. Now you page will be able to 'parse' PHP code. Add the following to you page: <?php echo date("d-m-Y"); ?> This will results in todays date appearing, i.e. "29-12-2008". Please note: You will need to upload your page to a server and view it through a browser in order for this to work. Dreamweaver, Golive, etc cannot 'parse' PHP information. You may also want to try 'WampServer' (www.wampserver.com) if you wish to view your file offline. Okay, we can see the date echoed-out on screen in a certain format, but how can we change this? Try the following: <?php echo date("Y-m-d"); ?> Now the order in which the date appears changes from i.e. "29-12-2008" to "2008-12-29". Very simple, isn't it. You may also want to change the way in which the date is display with regards to the length of the year or month number to actual month name. <?php echo "Today's date is ".date("d-m-Y")." (".date("jS F y")." .") and the time is ".date("i:G:s a"); ?> There are many ways of showing the date and time in PHP. For more examples and things to try, visit http://uk3.php.net/date. It is worth noting; time and date information will not update in 'real-time'. They will be whatever the time and date is at the moment the browser loads the page. In order to refresh the timestamp, you will need to refresh your browser. Changing the dateYou can add to or take time from you date in PHP. For instance, you might want to calculate the date in a week from now, this is a little more complex: <?php $days = 7; $seconds = $days * 86400; /// Don't change $timestamp = time(); $tm = $timestamp+$seconds; $c_date = date("F j, Y", $timestamp); echo "Today: $c_date <br>"; $f_date = date("F j, Y", $tm); echo "Future date: $f_date <br>"; ?> Formatting a string to dateYou may want to specify a particular date, and format it is a certain way. You can use strtotime to do this: <?php $mydate = "29-12-2009"; $refined_date = date("jS F Y", strtotime($mydate)); echo $refined_date; ?> Now the string '$mydate', containing '29-12-2009' will echo-out as '29th December 2008'. Credits & LinksArticle written by James Middleton - www.webdesign-4-beginners.co.uk.Information for PublishersThis article is copyrighted and you do not have consent to copy or redistribute it without written consent from the author.Add your own comments |
|
Web Design for Beginners is ©Copyright 2008 Turning Turnip Web Design - All rights reserved.
|