H I O X INDIA
Online PHP Tutorial
 HOME  ||  Scripts  ||  Purchase  ||  Tutorials  ||  Images  ||  Tools  ||  Templates 
  :-)  Send Page   :-)   Feedback   :-)   Register   :-)   Links   :-)   Support   :-)
Español Français 中文 Deutsch Portuguese Japanese தமிழ்
 Forums   Hosting   Internet Stats   Easy Calculation   FUN Games 

PHP Topics
Introduction
Syntax
Data Types
Variables
Operators
Control Structures
Functions
Pre-defined Function
Calendar Functions
Date and Time
Array Functions
Array Functions List1
Array Functions List2
Math Functions
PHP Mysql Functions
File Handling
Error Handling
DB Size
PHP Mail
String Tokens
String Functions
String Functions List1
String Functions List2
Session Functions
Cookies Functions
Form Variables
Running PHP from JS
Array To JS
Array to PHP
Encryption
Common Header
Ask Your Doubts
More about PHP
Feedback





Date and Time in PHP


Topic

How can I get current data and time in php?
My time is 7 hr faster then server time. How can I manipulate it?
Using php date and time?



Explanation

Date and Time using php?

The following portion has the code that can be used in php to get date and time

Knowing current date:

Example:
Tue, 09 Feb 2010 23:58:34 +0530

code:
<?php
$dat = date('r');
echo($dat);
?>

Here we use the date() funxtion of php which returns a string in specified format.
'r' states for unix format of date.

With date() function we can specify any format of date
Exmaple:
Feb-09-2010

code:
<?php
$dat = date('M-d-Y');
echo($dat);
?>

Here 'M' states for Month, 'd' for day, 'Y' for year.



Knowing past / future date based on time:

Getting the date of a nearest time. Say date of the time less than 5 hours of the current server time.

Example:
Date of time less than 5 hrs - Tue, 09 Feb 2010 18:58:34 +0530

code:
<?php
$date = date('r',time()-(3600*5));
echo "Date of time less then 5 hrs - $date";
?>

Here we use the time() and date() functions of php.
time() gives current time in milliseconds.
To get a date less then 5hrs, we reduce the time value by 5 hrs (i.e 5*3600 milliseconds).
We pass the format and milliseconds as argument to create the date for that time.




Getting Day/Date details of future dates

Day info of 13-5-2005
Example:

Fri, 13 May 2005 00:00:00 +0530
May-Fri-2005

For getting the values we use the function mktime() and date().
mktime() is used to create the long value for the date we wanted info for.
We pass argument for mktime as mktime(hour,min,sec,month,date,year)
The returned value of mktime is passed to date and required format of date was retrieved.
Code:
<?php
$mkt = mktime(0,0,0,5,13,2005);
$ds = date('r',$mkt);
echo($ds);

$ds = date('M-D-Y',$mkt);
echo($ds);
?>



Getting current time
By using the following code, we can get current time.
We use localtime() function of php. It returns an array.
From the array we get the datas.
Example:
23:58:34

Code:
<?php
$tim = localtime(time(),true);
echo($tim['tm_hour'].":".$tim['tm_min'].":".$tim['tm_sec']);
?>




privacypolicy     licence     sitemap
© 2004-2010 HIOX INDIA