Lost Password?


  #1 (permalink)  
Old 01-10-2007, 07:10 PM
John's Avatar   
John John is offline
Co-Administrator
 
Join Date: Jul 2006
Age: 20
Posts: 3,433
Last Blog:
Google Web Toolkit
Rep Power: 20
John has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond reputeJohn has a reputation beyond repute
Send a message via AIM to John Send a message via MSN to John
Default PHP:Tutorial The Date

The Idea
It is often a nice feature to show users the current date or calculate a date in the future dynamically. The idea used here could be helpful in a payment processing script where a persons payment is due one month from the start date.

The Solution
PHP has a native function for just this task, called date(). The date function accepts either one or two parameters: a string or a unix time stamp. Now one idea you have to understand, is since PHP is a server-sided programing language, the date being displayed is the date of the server, NOT of your local computer.

To display the date you could simply go like so:
PHP Code:
<?php
echo "Today is ";
echo 
date('Y-m-d');
?>
Which will display something like 2007-01-10. You can change the output of the date by changing the string parameters. For example you could do:

PHP Code:
<?php
echo "Today is " date('l')
?>
Today is Wednesday. To calculate a date in the future you must use the time() function in conjunction with the date() function. time() gives you the current unix time stamp which is the number of seconds since January 1st 1970 (dont quote me on that exact date). To calcuate one week from today you can just calculate the number of seconds in a week and add it to the time function then pass it in as a date() parameter.

PHP Code:
<?php
$nextWeek 
time() + (24 60 60);
echo 
"Next week will be ";
echo echo 
date('Y-m-d'$nextWeek);
?>
Rather than calculating the number of seconds there is another option for you. Say I want to print yesterdays date using strtotime() in coujunction with date we can accomplish this.
PHP Code:
<?php
echo date('d-m-Y'strtotime('-1 days')); 
?>
Similarly you can calculate last monday by doing this
PHP Code:
<?php
echo date('d-m-Y'strtotime('last monday'));
?>
For more information on characters that are recognized in the format parameter string see:
PHP: date - Manual
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Date in Oracle 8 Ronin Database & Database Programming 6 08-17-2007 01:25 AM
Translate date into your language Jaan PHP Tutorials 5 06-01-2007 06:23 PM
Displaying e-mail date kittenslayer Visual Basic Programming 0 04-01-2007 05:45 PM
Java.lang.string to mysql date reachpradeep Java Help 0 03-04-2007 09:08 AM
Finding Date NeedHelp C# Programming 3 12-08-2006 05:37 PM


All times are GMT -5. The time now is 05:06 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 97%

Ads