Create a website RSS feed

PHP Working With Dates

PHP working with dates

Last update on: 10-07-2008
We will now learn how to manipulate dates in various formats and how to display them.

Code to use with the date() function:


Format
Description
Example
a
"am" or "pm" lowercase
pm
A
"AM" or "PM" uppercase
PM
d
day of the month
01 /20
D
day of the week with 3 letters
mon
F
month's name
January
h
hours (12 hours format)
12
H
hours (24 hours format)
08
g
hours (12 hours format)
4
G
hours (24 hours format)
10
i
minutes
44
j
day of the month
3
m
month of the year
04
M
month of the year with 3 letters
oct
n
month of the year
4
s
seconds
30
y
year 2 numbers
08
Y
year 4 numbers
2008

Example: today's date:

 <?php
$today_date = date("m-d-Y");
print 'We are on '.$today_date;

//Out put on the screen 
?>
We are on 10-08-2008

System date format:

Example with the time() function

<?php
$today_date  = time();
print 'date system of today is '.$today_date;

//output 
?>

date system of today is 1012566581

Codes to use with the getdate() function:

key
description
Example
seconds
seconds
30
minutes
minutes
5
hours
day's hours from 0 to 23
15
mday
month's day from 1 to 31
12
wday
week's day from 0 to 6
2
mon
year's month
4
year
year with 4 numbers
2002
yday
year's day from 0 to 365
180
weekday
name of the week
Monday
month
month of the year
January

Example with getday():

<?php $time = time(); // the date system format
$date = getdate($time); // passage of the time() variable via getdate() to get the info print 'We are on '.$date['mon'].' - '.$date['mday'].' - '.$date['year'].' it is '.$date['hours'].':'.$date['minutes'];
//output on the screen
?>
we are on 10 - 08 - 08 it is 3:30

PHP and MySQL's lessons:

Introduction To PHP
Get Started With PHP
PHP Variables
PHP Variables Of Environment
PHP Conditions
PHP Looping
PHP Cookies
PHP Working With Dates
PHP Working With Arrays
PHP Working With Files
PHP Play With Strings
PHP And Forms
Send Emails With PHP
The Include Statement
Get Started With MySQL
MySQL Update And Delete
The WHERE Clause
MySQL Functions
Guestbook Script
Websites Directory Script
Multiple Pages With PHP
Create Your Forum With Php

Banner HomeServices Contact |  ©2009 http://www.iteachweb.net/