Formatting date, time and time zone with php
PHP
No Comments Saturday, March 7th, 2009
Formatting date, time and time zone with php is a very easy task. You may have a date
string in your database like (March 7, 2009, 5:16 pm), (03.10.01), (Sat Mar 10 15:16:08 MST 2001)
or as a timestamp like (1232886599) or something else. But you want to show this time in other
formats.
As example, you may have (March 7, 2009, 5:16 pm), but you want show this as 10-03-2001. So how
will you do this task? The answer is simple. Following code shows that
<?php
$date = 'March 7, 2009, 5:16 pm'; //date from your database
//make it as time stamp
$date = strtotime($date); //makes the date as timestamp
$date = date('d-m-Y',$date); // show as (07-03-2009)
echo $date;
?>
You can make any kind of formate with the timestamp. So, at first you need to convert the date
in timestamp.
Another Thing is to show a time in your time zone. How to do this?
<?php
$date = $some_gmt_date; //date from your database
//make it as time stamp
$date = strtotime($date); //makes the date as timestamp
date_default_timezone_set('Asia/Dhaka'); //this is for +6 with gmt
$date = date('d-m-Y',$date); // show as (07-03-2009)
echo $date;
?>
That was preety easy


No comments yet.