PHPBuilder - Date Manipulation in Php Page 3



RSS Twitter
Articles Php Functions

Date Manipulation in Php - Page 3

by: Allan Kent
|
July 30, 2000

Changing the way the date is displayed - date and time formatting

PHP supplies you with 2 ways of turning the Unix timestamp into something useful. The first one is a function called date(). This function takes two arguments - a string that lays out the formatting that you want returned, the second the Unix timestamp. The formatting string is simply some special characters that display those parts of the date and time that you want in the format that you want it. Suppose you wanted the date to be displayed as "18h01 Sunday 21 May"
We will need to use one of the special characters for each of the changing bits in the string- you can look these up in the PHP manual under function.date.html. There's a fair number of these characters and they will return things like the day of the week, month name, year in 2 and 4 digit format, whether it AM or PM and many others. The ones that we will need for this example are:
'H'which returns the hour of the day in 24-hour format
'i'which will return the minutes
'l'which will return the day of the week (long form)
'd'which returns the day of the month and
'F'the full month name
So our string will look something like "Hhi l d F", and our PHP a bit like:

<?php

echo date'Hhi l d F'time() );

?>
When we run this though, we find that we actually get the result of
180609 Sunday 21 May
which seems a bit curious until we have a look at the PHP manual and find that the lowercase h stands for the hour in 12 hour format. So once again, the old saying of "A computer does what you tell it to do, not what you want it to do" rings true. We have two options here. The first is to escape the lowercase h and have this:

<?php

echo date'H\hi l d F'time() );

?>
which gives us the result:
18h12 Sunday 21 May
This is great, but what happens if we want to include some date and time info into a more complicated sentence? Do we have to escape every character? No, we use a different function, one called strftime().
strftime() brings with it 2 advantages. The first which we are not going to cover here is that if you have used the setlocale() function, you can have strftime bring the month names up in the correct language. The other advantage is that you can embed your specially formatted date and time info into free-form strings. This does mean however that if you went and learned all of the special characters for the date() function, you get to learn a whole set of different ones now.
The way that strftime() works is exactly the same as date(), except that the special characters have the percent sign % in front of them. So the previous example in strftime() would look like

<?php

echo strftime'%Hh %M %A %d %b'time() );

?>
which gives us:
18h24 Sunday 21 May
This may seem a trifle more complicated, but imagine what you actually wanted to say was "Today is Sunday 21 May 2000. The time is somewhere close to 18h24." Using the date command in this instance would be a bit annoying I think.
Earlier I mentioned that there were 2 ways of getting something useful out of the Unix timestamp. We've just seen date() and strftime(). The other is getdate(). This function only needs the Unix timestamp as an argument and it returns an associative array of the elements in the date.
As an example:

<?php

$date_time_array 
getdatetime() );
echo 
$date_time_array['weekday'];

?>
will return:
Sunday
Besides "weekday", the other parts of the array are:
"seconds"seconds
"minutes"minutes
"hours"hours
"mday"day of the month
"wday"day of the week as a number
"mon"month as a number
"year"year
"yday"day of the year as a number
"month"full month name
So now we can create readable dates and times. But what about going the other way ?


« Previous Page
1
|
2
|
3
|
4
|
5
|
6
|
7
Next Page »

Comment and Contribute

Your comment has been submitted and is pending approval.

Author:
Allan Kent

Comment:



Comment:

(Maximum characters: 1200). You have characters left.