Date time is very extremely important if we will build critical application, so we must special attention for it. Handling date time properly in our program is more complicated than handling strings or numbers variable. Because date time is not a single value but a collection of values, such day, month, year, hour, minute and second. In this discussion, we will share the best practical, how we handling date time in our application.

In the programming convention, date time calculation is to treat a particular time and date as a single value, the number of seconds that have elapsed since midnight on January 1, 1970. It’s called an epoch timestamp. This convention will be a reference in subsequent calculation of date time. In the Microsoft Windows operating system accepts timestamps form zero to the largest 32-bit integer, which corresponds to January 19, 2038. in the *nix operating system, such Unix, BSD and Linux, allows for negative timestamp, which stretch back to December 13, 1901.

The best practice if we will calculation date time, we must store date time in a format called a timestamp. Why we must use a timestamp format ..? Because date and time are stored by computer in a format timestamps and PHP converts date and time from our notation into a timestamps format. Others reason for this decision, a timestamps format will store a variable date time in the integer value, so we more easily to calculate the time between two dates, just subtract one timestamp form the other. PHP has many functions built-in that we can use to handle date time variables.

If we use database in our application and use form to get input form user, the best practice for this case, we also store date time variable into database in a format timestamp. With this solution, we will solve many problems of different format between database sever and web server. See our example bellow to get more detail about this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 
	<?php
 
		/*  
by default, PHP store date time in timestamp format 
			output : 1258251480  at November 15, 2009 9:18:00 
		*/
 
		$today = time();
 
		/*
			Calculate between two date time
		*/
		$specialDate = strtotime(‘January 15, 2009);
		$timespan = $today - $specialDate;
		echo $timespan;
 
		/*
			If use database MySQL server to store date time
			Type field : integer(11)
		*/
		$query ( ‘insert into tb_log (‘date_created, event’) values ($today, ‘Store Date Time in timestamp format’);
 
?>

So, we will explain built in functions in PHP such how to display date time variables, how the best practice with form and date time inputs and others. The first, we will display of date time variables with the date() and strftime() function, as shown in code bellow.

1
2
3
4
5
6
7
8
9
10
11
12
13
 
	<?php
		/* display current time with strftime() function */
 
		print “Today is :.strftime(%m/%d/%Y”);
		/* output : Today is : 11/15/2009 */
 
		/* display current time with date() function */
 
		print “Today is :.date(“m/d/Y”);
		/* output : Today is : 11/15/2009 */
 
	?>

See more detail about parameters in strftime() and date() in official PHP site.

If we work with form to get date time values in our program as timestamp format, we need to convert other time representations to timestamp format. We can use mktime() function.

1
2
3
4
5
6
7
 
	<?php
 
		/* Get values current date time via POST Form */
		$today = mktime($_POST['hour'], $_POST['minute'], 0, $_POST['month'],$_POST['day'],$_POST['year']);
 
	?>

At the end of this discussion, we will explain that in PHPv5.1, PHP added a setting for a default local time zone to php.ini. To set a default time zone, edit php.ini file, in the [Date] section change date.timezone =, and add a time zone code after the equal sign. See example bellow.

1
2
3
4
5
6
7
8
9
10
 
	[Date]
; Defines the default timezone used by the date functions
date.timezone = America/Los_Angeles
 
;date.default_latitude = 31.7667
;date.default_longitude = 35.2333
 
;date.sunrise_zenith = 90.583333
;date.sunset_zenith = 90.583333

If we don’t have access to the php.ini file, we can set a default time zone in each script that applies to that script only, like script :

1
2
3
4
5
6
7
8
9
10
11
 
	<?php
		/* Get default time zone */
		$tzDefault = date_default_timezone_get();
		echo $tzDefault;   
 
 
		/* Set default time zone */
		date_default_timezone_set(“America/Los_Angeles”);
 
	?>

We can explore others date time function in PHP, see more detail at official PHP site or PHP manual. See next discussion in this site.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Netvouz
  • DZone
  • ThisNext
  • MisterWong
  • Wists
  • Slashdot
  • Technorati
  • YahooMyWeb