Go Back   Web Design & SEO Company > Knowledgebase

Knowledgebase Articles and information about running a website, cPanel and various hints and tips. Here you will find tutorials on php, MySql, .htaccess, cron, SEO, Search Engines, CHMOD, FTP, CSS, HTML and various other hints and tips on running and Administrating a website.

Reply
 
  #1  
Old 04-20-2007, 11:53 AM
Admin's Avatar
Administrator
 
Join Date: Jan 2007
Location: Taree
Posts: 607
Default Detailed Timestamp Information - Tutorial

Date and Time Functions

There are several PHP library functions that work with dates and times. Most either generate a Unix timestamp or format a Unix timestamp in a human-readable form.

Generating a Timestamp

Date and time is generally represented as a Unix timestamp: the number of seconds since 1 January 1970 00:00:00 Greenwich Mean Time. Most systems represent a timestamp using a signed 32-bit integer, allowing a range of dates from December 13, 1901 through January 19, 2038.

While timestamps are convenient to work with in scripts, care must be taken when manipulating timestamps to avoid integer overflow errors. A common source of errors is to compare two timestamps in which the date range is greater than the largest positive integer-a range just over 68 years for a signed 32-bit integer.

PHP gives unexpected results when comparing two integers that differ by an amount greater than the largest positive integer, typically 231-1. A safer way to compare large integers is to cast them to floating-point numbers. The following example illustrates this point:

Quote:
$var1 = -2106036000; // 16/08/1902
$var2 = 502808400; // 24/08/1984

// $result is assigned false
$result = $var1 < $var2;

// $result is assigned true as expected
$result = (float) $var1 < (float) $var2;
Even floating-point numbers can overflow. To manipulate numbers of arbitrary precision, the BCMath library should be considered.

Current time

PHP provides several functions that generate a Unix timestamp. The simplest:

Quote:
integer time( )
returns the timestamp for the current date and time, as shown in this fragment:

Quote:
// prints the current timestamp: e.g., 1008553254
echo time( );
Creating timestamps with mktime( ) and gmmktime( )

To create a timestamp for a past or future date in the range December 13, 1901 through January 19, 2038, the mktime( ) and gmmktime( ) functions are defined:

Quote:
int mktime(int hour, int minute, int second, int month, int day, int year
[, int is_dst])
int gmmktime(int hour, int minute, int second, int month, int day, int year
[, int is_dst])
Both create a timestamp from the supplied components; the parameters supplied to gmmktime( ) represent a GMT date and time, while the parameters supplied to mktime( ) represent the local time. This example creates a timestamp for 9:30 A.M. on June 18, 1998:

Quote:
$aDate = mktime(9, 30, 0, 6, 18, 1998);
Both functions are reasonably tolerant of zero values, and both correctly handle values out-of-range, allowing scripts to add a quantum of time without range checking. If the components of a date are outside the range of dates the function is defined for, -1 is returned. The following example shows how 30 days can be added to a date and time:

Quote:
$paymentPeriod = 30; // Days

// generates a timestamp for 26 June 2002 by
// adding 30 days to 27 May 2002
$paymentDue =
mktime(0, 0, 0, 5, 27 + $paymentPeriod, 2002);

// A different approach adds the appropriate number
// of seconds to the timestamp for 27 May 2002
$paymentDue = mktime(0, 0, 0, 5, 27, 2002)
+ ($paymentPeriod * 24 * 3600);
Both functions allow the supplied date to be interpreted as daylight savings time by setting the flag is_dst to 1.
The order of the arguments to these functions is unusual and easily confused. While the mktime( ) and gmmktime( ) functions are similar to the Unix mktime( ) function, the arguments aren't in the same order.

String to timestamp

This function generates a timestamp by parsing the human-readable date and time-between December 13, 1901 and January 19, 2038-from the string time:

Quote:
integer strtotime(string time)
The function interprets several standard representations of a date, as shown here:

Quote:
// Absolute dates and times
$var = strtotime("25 December 2002");
$var = strtotime("14/5/1955");
$var = strtotime("Fr1, 7 Sep 2001 10:28:07 -1000");

// The current time: equivalent to time( )
$var = strtotime("now");

// Relative times
echo strtotime("+1 day");
echo strtotime("-2 weeks");
echo strtotime("+2 hours 2 seconds");
Care should be taken when using strtotime( ) with user-supplied dates. It's better to limit the use of strtotime( ) to cases when the string to be parsed is under the control of the script, for example, checking a minimum age using a relative date:

Quote:
// date of birth: timestamp for 16 August, 1983
$dob = mktime(0, 0, 0, 16, 8, 1982);

// Now check that the individual is over 18
if ((float)$dob < (float)strtotime("-18 years"))
echo "Legal to drive in the state of Victoria";
Note that both timestamps are cast to floating-point numbers before comparing them to avoid the integer overflow problem highlighted earlier.

Subsecond times

While a Unix timestamp represents a date and time accurate to the second, many applications require times to be represented to the subsecond. PHP provides the function:

Quote:
string microtime( )
This returns a string that contains both a Unix timestamp in seconds and a microsecond component. The returned string begins with the microsecond component, followed by the integer timestamp:

Quote:
// prints the time now in the format "usec sec"
// e.g., 0.34783800 1008553410
echo microtime( );
One common use of the function microtime( ) is to generate the seed for a random-number generator:

Quote:
// Generate a seed.
$seed = (float)microtime( ) * 100000000;

srand($seed);
Because the microsecond component appears at the start of the string returned from microtime( ), the returned value can be converted to a float with the (float) cast operator. Multiplying the float result by 100,000,000 ensures that you pass a suitably varying integer to the seeding function srand( ). Random-number generation is covered in more detail in Section 2.9.

Formatting a Date

While the Unix timestamp is programmatically useful, it isn't a convenient display format. The date( ) and gmdate( ) functions return a human-readable formatted date and time:

Quote:
string date(string format [, integer timestamp])
string gmdate(string format [, integer timestamp])
The format of the returned string is determined by the format argument. A predetermined date can be formatted by passing in the optional timestamp argument. Otherwise, both functions format the current time. The format string uses the formatting characters listed in Table 2-3 to display various components or characteristics of the timestamp. To include the characters from the table, the backslash character is used. The following examples show various combinations:

Quote:
// Set up a timestamp for 08:15am 24 Aug 1964
$var = mktime(8, 15, 25, 8, 24, 1964);

// "24/08/1964"
echo date('d/m/Y', $var);

// "08/24/64"
echo date('m/d/y', $var);

// "Born on Thursday 24th of August"
echo date('\B\o\r\n \o\n l jS \of F", $var);
Table 2-3. Formatting characters that represent various date and time components

http://www.tareeinternet.com/forum/images/timestamp.jpg

(Click to view table image 2-3)

PHP also provides the equivalent functions:

Quote:
string strftime(string format [, integer timestamp])
string gmstrftime(string format [, integer timestamp])
The format string uses the same formatting character sequences as the C library function strftime( ).

Validating a Date

The function checkdate( ) returns true if a given month, day, and year form a valid Gregorian date:

Quote:
boolean checkdate(integer month, integer day, integer year)
This function isn't based on a timestamp and so can accept a larger range of dates: basically any dates in the years 1 to 32767. It automatically accounts for leap years.

Quote:
// Works for a wide range of dates
$valid = checkdate(1, 1, 1066); // true
$valid = checkdate(1, 1, 2929); // true

// Correctly identify bad dates
$valid = checkdate(13, 1, 1996); // false
$valid = checkdate(4, 31, 2001); // false

// Correctly handles leap years
$valid = checkdate(2, 29, 1996); // true
$valid = checkdate(2, 29, 2001); // false
Hope this info assists with your Timestamp questions, we will be updating this with further information.
Reply With Quote
Top SEO Tool
Harvester and Mass Blog Commenter
Blog Comment Software

  #2  
Old 12-08-2008, 01:40 AM
Junior Member
 
Join Date: Dec 2008
Location: Dubai, United Arab Emirates www.dubai-mobiles.net
Posts: 28
Default

So much just for date ? lol
anyways good work.
Reply With Quote
Top SEO Tool
Harvester and Mass Blog Commenter
Blog Comment Software

Reply

Tools
Display Modes

Similar Threads
Thread Thread Starter Forum Replies Last Post
Information From an SEO Client Admin SEO 33 12-19-2011 09:09 AM
Meta Tag Information Admin SEO 11 11-26-2008 09:58 PM
Information about Directory Tisankit SEO 5 09-19-2008 04:25 AM
SEO tutorial criphaze22 SEO 5 09-05-2008 03:47 AM



Knowledgebase | SEO | Free Scripts | Free Wordpress Themes | Free Graphics

Micro Niche Finder Review - eval gzinflate base64 decode


Forum time zone is GMT. Currently it's 08:33 AM.


Forum SEO | Chrome Plugins

Web Design & SEO Forums