|
|
|
Dates and Times Fundamentals
============================
One of the most important aspects of Internet programming is Dates and Times. In
this article we will learn how to manipulate dates and times.
PHP getdate() function returns an associative array of getdate(timestamp) ,
evaluates local time if no timestamp given.seconds,minutes,hoursmday-Day of
month ,wday-Day of Weekmon-Current Month Number,year- Numberyday-day of year as
number,weekday-day of week in text form ,month-month of the year in text form.
<?php
$date_ar=getdate();
print_r($date_ar);
?>
This function is very useful it enables us to format date dd/mm/yyyy
<?php
$date_1=getdate();
$date_2="$date_1[mday]/$date_1[mon]/$date_1[year]";
echo $date_2;
?>
Checking Validity of a Date
===========================
use the checkdate() function which validates date in the format MM/DD/YYYY
LIST
====
void list (...) Like array(), this is not really a function, but a language
construct. list() is used to assign a list of variables in one operation.
Explode
=======
array explode (string separator, string string [, int limit]) Returns an array of
strings, each of which is a substring of string formed by splitting it on boundaries
formed by the string separator. If limit is set, the returned array will contain a
maximum of limit elements with the last element containing the rest of
string.checkdate() function cont ...
<?php
$date1="1/15/2002"; //mm/dd/yyyy
list($month,$day,$year)=explode("/",$date1);
if (checkdate($month,$day,$year)){print "Valid Date !!!";
}else{ print "Not Valid !!!";}
?>
mktime
======
Get UNIX timestamp for a date Description int mktime (int hour, int minute, int
second,
int month, int day, int year [, int is_dst]) Is_dst can be set to 1 if the time is
during
daylight savings time, 0 if it is not, or -1 (the default) if it is unknown whether
the time
is within daylight savings time or not.
<?php
$date1="1/15/2002";/mm/dd/yyyy
list($month,$day,$year)=explode("/",$date1);
$timestamp1=mktime(0,0,0,$month,$day,$year);
echo $timestamp1;
?>
Formatting Timestamps
=====================
The easiest way of formatting UNIX timestamp is to use date() or strftime function
<?php
$date1=date("l F j",504107800);
echo $date1;
$date1=strftime("%A %B %d",504107800);
echo $date1;
?>
The following characters are recognized in the format string:
* a - "am" or "pm"
* A - "AM" or "PM"
* B - Swatch Internet time
* d - day of the month, 2 digits with leading zeros; i.e. "01" to "31"
* D - day of the week, textual, 3 letters; i.e. "Fri"
* F - month, textual, long; i.e. "January"
* g - hour, 12-hour format without leading zeros; i.e. "1" to "12"
* G - hour, 24-hour format without leading zeros; i.e. "0" to "23"
* h - hour, 12-hour format; i.e. "01" to "12"
* H - hour, 24-hour format; i.e. "00" to "23"
* i - minutes; i.e. "00" to "59"
* I (capital i) - "1" if Daylight Savings Time, "0" otherwise.
* j - day of the month without leading zeros; i.e. "1" to "31"
* l (lowercase 'L') - day of the week, textual, long; i.e. "Friday"
* L - boolean for whether it is a leap year; i.e. "0" or "1"
* m - month; i.e. "01" to "12"
* M - month, textual, 3 letters; i.e. "Jan"
* n - month without leading zeros; i.e. "1" to "12"
* O - Difference to Greenwich time in hours; i.e. "+0200"
* r - RFC 822 formatted date; i.e. "Thu, 21 Dec 2000 16:01:07 +0200" (added in PHP
4.0.4)
* s - seconds; i.e. "00" to "59"
* S - English ordinal suffix, textual, 2 characters; i.e. "th", "nd"
* t - number of days in the given month; i.e. "28" to "31"
* T - Timezone setting of this machine; i.e. "MDT"
* U - seconds since the epoch
* w - day of the week, numeric, i.e. "0" (Sunday) to "6" (Saturday)
* Y - year, 4 digits; i.e. "1999"
* y - year, 2 digits; i.e. "99"
* z - day of the year; i.e. "0" to "365"
* Z - timezone offset in seconds (i.e. "-43200" to "43200").
The offset for timezones west of UTC is always negative, and for those east of UTC
is always positive. Unrecognized characters in the format string will be printed as-
is. The "Z" format will always return "0" when using gmdate().
strtotime
=========
This function tries to figureout the format of the date in the input string and
return a timestamp for it. If you omits some part of date it will use the current
time and date for the reminder. It is also useful in calculating timestamps for
relative offsets.
<?php
$date1=mktime(0,0,0,07,08,2000);
$date2=strtotime("+40 days",$date1);
$date2=date("l F Y",$date2);
echo $date2;
?>
<?php
$date1=mktime(0,0,0,07,08,2000);
$date2=strtotime("+4 months +5 days",$date1);
$date2=date("l F Y",$date2);
echo $date2;
?>
Halting Program Execution
=========================
<?php
print "Hallo Word";
sleep(10); // Sleep 10 seconds
usleep(70);//Sleep 70 mircoseconds
print "This was done after 10 minutes and 70 microseconds.
?>
|
|
| A Timing Class Categories : PHP, PHP Classes, Date Time | | | Finding the day of the week for a specific date.
Categories : PHP, Databases, MySQL, Date Time | | | A wrapper function to format dates coming from a databases with the
same syntax as PHP's date() function. Categories : Date Time, Databases, PHP | | | pcCalendar class - Allows for the creation of calendars in HTML pages. All output functions can be easily overridden, refer to article 1471 for an example.
Categories : PHP, Date Time, Calendar, PHP Classes | | | A PHP Calendar function with CSS : add a cool calendar to any php page by just adding a calendar class based function. Categories : PHP, PHP Classes, Calendar, Date Time | | | PHP Round Clock - Must have Gif support to use this. Categories : PHP, Date Time, Graphics | | | A simple and fast calendar combining PHP and tables. Use this as a base for applications in which a calendar is needed. Categories : Date Time, PHP, Complete Programs, Calendar | | | Open and Close your website in fixed times . Categories : PHP, PHP Classes, Cron, Date Time | | | mysql date/time converters Categories : PHP, MySQL, Databases, Date Time | | | Creates three SELECT form fields: Month, Day, and Year. You give it a string which will be used to make the name for the three fields, and a number of seconds to use as the default date. If you give it blank for this value, the current date is used. Categories : HTML and PHP, PHP, Date Time | | | If you want to create select buttons featuring current date this example will show you how... Categories : Date Time, HTML and PHP, PHP | | | Example of using the pcCalendar class, article 1468 on weberdev.com. Calendar example. Categories : PHP, Date Time, PHP Classes, Calendar | | | calculus of the eastersunday Categories : BC math, PHP, Date Time, Databases | | | Functions used to define a schedule of holidays. Can define non-fixed holidays (eg. 3rd sunday of June). Categories : Calendar, Date Time, PHP | | | Monthly and Daily Upcoming Events calendar. Categories : Date Time, PostgreSQL, PHP, Calendar, Databases | |
|
|
|