PHP Tutorial





Español Français 中文 Deutsch Portuguese Japanese nederlands
   
 
PHP Topics
Introduction Introduction
Syntax Syntax
Data Types Data Types
Operators Operators
Control Structures Control Structures
Functions Functions
Pre-defined Function Pre-defined Function
Calendar Functions Calendar Functions
Date and Time Date and Time
Array Functions Array Functions
Array List Array Functions List1
Array Function List Array Functions List2
Math Functions Math Functions
PHP MYSQL Functions PHP Mysql Functions
File Handling File Handling
Error Handling Error Handling
DB Size DB Size
PHP Mail PHP Mail
String Tokens String Tokens
String Functions String Functions
String Functions List String Functions List1
String Functions List2 String Functions List2
Session Functions Session Functions
Cookies Functions Cookies Functions
Form Variables Form Variables
Running PHP from JS Running PHP from JS
Array To JS Array To JS
JS Array Array to PHP
Encryption Encryption
Common Header Common Header
Forums Ask Your Doubts
Scraps More about PHP
Feedback Feedback
 




Date and Time in PHP


Tutorials »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:
Sat, 04 Feb 2012 01:26:26 -0500

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-04-2012

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 - Fri, 03 Feb 2012 20:26:26 -0500

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 -0400
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:
1:26:26

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





A Note

Learn PHP programming language tutorial with simple and neat example. Hope you enjoy this free tutorial. Do give us your valuable feedback and suggestions on this online tutorial. This is a Copyright Content.

Other Links

web hosting