Monday, December 16, 2013
Using PHP Date() function to display Date and Time in Different Formats
Monday, December 16, 2013 by Unknown
Hiiii, We all know that managing Dates and Dates formats is a very important work when it comes to using dates in programming or when you have to use the dates fetched from the database in the format other then as it is saved in database.
In PHP, some people mostly beginners find it difficult to deal with date and time with different formats like 24 hour or 12 hour, some find it difficult to get current date/ time, you current timezone with GMT difference.
Mostly people face trouble in the situation where a person has to change the format of the the data fetched from database or date got from the form fields.
So, here in this post I'll be telling you how to use PHP Date() with its different forms to gets every date time related data as possible like- current date/ time, custom date with different formats, custom time with different formats and changing the format of date fetched from the database .
DEMO BEGINS :
<?php
// Connection to mysql
$link = mysql_connect('localhost', 'root', 'root');
if (!$link) {
die('Not connected : ' . mysql_error());
}
// Selecting Database
$db_selected = mysql_select_db('wordpress', $link);
if (!$db_selected) {
die ('Can\'t use wordress : ' . mysql_error());
}
$query="select wp_posts.post_date from wp_posts where wp_posts.ID=27";
$line=mysql_query($query);
$result=mysql_fetch_assoc($line);
$post_date=$result['post_date'];
echo $post_date."<br/>"; // prints in this format- 2013-10-31 07:59:13
echo date('d M Y', strtotime($post_date))."<br/>"; // prints in this format- 31 Oct 2013
echo date('d/m/y', strtotime($post_date))."<br/>"; // prints in this format- 31/10/13
echo date('D', strtotime($post_date))."<br/>"; // prints in this format- Thu
// Taking Custom Dates
$date="2013-01-07";
echo date('d M Y', strtotime($date))."<br/>"; // prints in this format- 31 Oct 2013 with leading zeros
echo date('j M Y', strtotime($date))."<br/>"; // prints in this format- 31 Oct 2013 without leading zeros
// Time notations
echo date('H:i:s', strtotime($post_date))."<br/>"; // prints in this format- 07:59:13
echo date('H:i:s a', strtotime($post_date))."<br/>"; // prints in this format- 07:59:13 am
echo date('H:i:s A', strtotime($post_date))."<br/>"; // prints in this format- 07:59:13 AM
// Taking Custom TIme
$time="15:30:41";
echo date('h:i:s A', strtotime($time))."<br/>"; // prints in this format- 03:30:41 PM in 12h format
echo date('H:i:s A', strtotime($time))."<br/>"; // prints in this format- 03:30:41 PM in 24h format without trailing zeros
echo date('g:i:s A', strtotime($time))."<br/>"; // prints in this format- 03:30:41 PM in 12h format
echo date('G:i:s A', strtotime($time))."<br/>"; // prints in this format- 03:30:41 PM in 24h format without trailing zeros
echo date('O')."<br/>"; // prints the Difference to Greenwich time (GMT) in hours
// Current Date and Time
echo "Current Date :".date('d M Y')."<br/>";
echo "Current Time :".date('h:i:s A');
?>
Hope you like this post……..Please Comment…………!!!!!!!!!Tags: PHP
Subscribe to:
Post Comments (Atom)
0 Responses to “Using PHP Date() function to display Date and Time in Different Formats”
Post a Comment