FireFox! The PHP Forum Loans and Credit
Panama Web Design for Hire Free Insurance Quotes!
Web Hosting Advertise Here $10 a Month Designer Children
Never Pay Taxes Again HGH Domain name registration
Web Hosting and Dedicated Servers Insurance Affordable web-hosting


HomeWatched TopicsRegisterSearchDirectory
FAQMemberlistUsergroupsLog inStoresItemsBank
Google

Reply to topic Page 1 of 1
Working with dates and times in PHP
Message  

Reply with quote
Post Working with dates and times in PHP 
Introduction
Let’s discuss dates and times and how we use them in PHP projects. The date and time functions allow you to use the time of the server that PHP is running on so keep in mind that this function will depend on the local setting of your server.

First Step
Now, the first step is we have to know what value the time function in PHP produces. Run this code on your server:

<?php
print time();
?>  


You will see a line of numbers in your browser. That is the number of seconds since the epoch. The epoch was on January 1st 1970. This line of numbers is called a UNIX timestamp.

Source: http://codewalkers.com/tutorials/29/1.html

View user's profile Send private message

Reply with quote
Post  
Human Readable Formats
Now, we want to display that timestamp in a human readable format. To do that we have to understand the date() function. This date function is used to convert from a UNIX timestamp to a human readable date. The date function is:

string date ( string format [, int timestamp])  


You can see that the timestamp is rounded by ‘[‘ and ‘]’, that means it’s optional. If we put it then the function will use it and if we don’t put it then the function will use the default timestamp. The default timestamp which is used when we don't put any timestamp is the current time.

Let’s start our work with the date function. We want to show the user what date and time is now.

<?php
print date("l, F jS Y – H:i:s");
?>  


As you can see on the example above, I use l, F jS Y – H:i:s as the variable passed to the function. Here is a list of formatting characters used:

F - month, textual, long; e.g. "January"
H - hour, 24-hour format; i.e. "00" to "23"
i - minutes; i.e. "00" to "59"
j - day of the month without leading zeros; i.e. "1" to "31"
l (lowercase 'L') - day of the week, textual, long; e.g. "Friday"
s - seconds; i.e. "00" to "59"
S - English ordinal suffix for the day of the month, 2 characters; i.e. "st", "nd", "rd" or "th"
Y - year, 4 digits; e.g. "1999"  

Source: http://codewalkers.com/tutorials/29/2.html

View user's profile Send private message

Reply with quote
Post  
Making your own date and time
Are you satisfied now that you can show your user for the current date and time? If you do, then don’t need to read this article for more. The next step is for people who need something more.

The next function that we have to understand is mktime(). This function will make your own timestamp from a given set of variables that you pass to the function.

int mktime ( int hour, int minute, int second,
     int month, int day, int year [, int is_dst])  


Now, I want to know in what day my birthday was on. My birth date is July 21st 1974. I’ll use the mktime function like so:

<?php
$mybirthdate = mktime(0,0,0,7,21,1974);
print date("l", $mybirthdate);
?>  


Wow, I finally found out that my birthday is on Sunday. Smile. Now the next case I want to know is what day is 25 days from now. Let’s do it this way:

<?php
$next25day = mktime(0,0,0,date("n"),date("j")+25,date("Y"));
print date("l", $next25day);
?>  


Give attention to the variables that I passed to the mktime function. I’ve passed date("j")+25. It means that I want to add 25 to the current date. You can figure out what it will produce.

Another method
This is another method to access the current date or the given timestamp that you’ve produced by your own script. The method, or you can call it function, is getdate(). This function will produce an associative array containing the date information. The array key is:

"seconds" - seconds
"minutes" - minutes
"hours" - hours
"mday" - day of the month
"wday" - day of the week, numeric: from 0 as Sunday up to 6 as Saturday
"mon" - month, numeric
"year" - year, numeric
"yday" - day of the year, numeric; i.e. "299"
"weekday" - day of the week, textual, full; i.e. "Friday"
"month" - month, textual, full; i.e. "January"  


Example Usage:

<?php
$today = getdate();
print $today["month"] // it will print the month of current date
print $today["weekday"] // it will print the day of current date
?>  

Source: http://codewalkers.com/tutorials/29/3.html

View user's profile Send private message

Reply with quote
Post  
My own Language
By this time I hope you can create your own date and time using the timestamp of the current date or your own timestamp. I’m not using English as my primary language, and neither are my website users. We are from Indonesia and I want to display the date in Indonesian language. How can I do it? That’s a perfect question and asked by most beginners.

Let’s create an associative array of date and month in our own language.

<?php
$days = Array ("Minggu", "Senin", "Selasa", "Rabu", "Kamis", "Jum’at", "Sabtu");
$months = Array (1=>"Januari",
                 2=>"Pebruari",
                 3=>"Maret",
                 4=>"April",
                 5=>"Mei",
                 6=>"Juni",
                 7=>"Juli",
                 8=>"Agustus",
                 9=>"September",
                 10=>"Oktober",
                 11=>"Nopember",
                 12=>"Desember");
?>  


After we create our own language of date and months, now it’s time to display it.

<?php
print $days[date("w")]; // display name of day with our own language
print $months[date("n")];
?>  


Source: http://codewalkers.com/tutorials/29/4.html

View user's profile Send private message
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
  



Google

FireFox! The PHP Forum Loans and Credit
Panama Web Design for Hire Free Insurance Quotes!
Web Hosting Advertise Here $10 a Month Designer Children
Never Pay Taxes Again HGH Domain name registration
Web Hosting and Dedicated Servers Insurance Affordable web-hosting


Web Design by PlatinumShore.com & Web Hosting by TradeWebHosting.com