For smart Primates & ROBOTS (oh and ALIENS ).

Blogroll

Thursday, January 15, 2015

Get current right time in php

Problem:

Some times when you print current date using  $mydatetime=date("Y-m-d H:i:s"); or print current time using echo $mytime = date("H:i:s");  in php then you will wonder that it will show wrong date time to you. so how you will show correct current date time in php?

Solution:

PHP will not set by default your current timezone, so that's the reason it will show you wrong date time. to show correct current date time in php you need to set your timezone by date_default_timezone_set function.

Example:

Suppose you are Indian then you will use:
date_default_timezone_set('Asia/Culcutta');
$curr_date_time = date("Y-m-d H:i:s");
echo $curr_date_time;
$curr_time = date("H:i:s");
echo $curr_time;
Suppose you are American then you will use:
date_default_timezone_set('America/Los_Angeles');
$curr_date_time = date("Y-m-d H:i:s");
echo $curr_date_time;
$curr_time = date("H:i:s");
echo $curr_time;


and so on set as your timezone as you require.

Share:

0 comments:

Post a Comment

Add Dependent Scripts

   here is the code that allow to add dependent script :         <script>         const addDependentScripts = async function( scriptsT...

Ads Inside Post

Powered by Blogger.

Arsip