Indian Date and time in php - php

Guys I am trying to get correct Indian time and date in PHP
I have tried this code:
$date = date("d/m/Y");
$date1 = date("H:i a");
if(function_exists('date_default_timezone_set'))
{
date_default_timezone_set("Asia/Kolkata");
}
echo $date;
echo $date1;
But I am not getting correct time. I am getting time which is 4.30 Hours late. Is there any mistake in my code?

Put the timezone declaration first before using any date function:
// set the timezone first
if(function_exists('date_default_timezone_set')) {
date_default_timezone_set("Asia/Kolkata");
}
// then use the date functions, not the other way around
$date = date("d/m/Y");
$date1 = date("H:i a");
echo $date . '<br/>';
echo $date1;

The cleaner option is
$date = new DateTime(null, new DateTimezone("Asia/Kolkata"));
echo $date->format('d/m/y').'<br/>';
echo $date->format('H:i a');
This way, you wouldn't alter global state, so other pieces of the code can still use other timezones

<?php
date_default_timezone_set("Asia/Kolkata");
echo "The date is " .date("d/m/y");
echo "<br/>";
echo "The time is " . date("h:i:sa");
?>

I think you should try this
date_default_timezone_set('Asia/Kolkata');
and
$date = date('Y-m-d H:i:s');
what ever format you like!!

// set the timezone first
if(function_exists('date_default_timezone_set')) {
date_default_timezone_set("Asia/Kolkata");
}
// then use the date functions, not the other way around
$date = date("d/m/Y");
$date1 = date("H:i a");
echo $date . '<br/>';
echo $date1;

Related

Get the current time and date by using DateTime::createFromFormat

How can I get the current date and time for this time zone 'America/phoenix'? I can't figure out how this can be done with the DateTime::createFromFormat.
My Code example
<?php
$format = 'n-j-Y g:iA';
$date = DateTime::createFromFormat($format, '/*Example: 6-12-2019 12:05am*/', new
DateTimeZone('America/phoenix'));
echo $date->format('n-j-Y g:iA') . "\n";
?>
You can change the second parameter of createFromFormat, but from your question, I guess you are not America/phoenix, therefore, use this code:
<?php
date_default_timezone_set('America/phoenix');
$currentTimeInPhoenix = date('n-j-Y g:iA');//gets the currenttime in phoenix
$format = 'n-j-Y g:iA';
$date = DateTime::createFromFormat($format, $currentTimeInPhoenix, new DateTimeZone('America/phoenix'));
echo $date->format('n-j-Y g:iA') . "\n";
date_default_timezone_set('YOUR OLD TIMEZONE');
?>

How to convert current IST time to UTC ? Also need to find the time one hour before that UTC time

Need to convert the current time in IST (India Standard Time), stored in a PHP variable to UTC time, and store it to another variable using PHP.
date_default_timezone_set('Asia/Kolkata');
$Present = date('Y-m-d H:i');
echo $Present; //to be converted to utc
$GMT = gmdate('Y-m-d H:i', strtotime($Present));
date_default_timezone_set('Asia/Kolkata');
$Present=date('Y-m-d H:i');
echo $Present; //to be converted to utc
$date = new DateTime($Present);
$date->setTimezone(new DateTimeZone('Africa/Abidjan'));
echo $date->format('Y-m-d H:i');
Use gmdate
echo gmdate('Y-m-d H:i', strtotime($Present));
following will decrease 1 hour
echo gmdate('Y-m-d H:i', strtotime($Present.'-1 hour'));
Try the php built in getTimezone and setTimezone,
$the_date = strtotime("2010-01-19 00:00:00");
echo(date_default_timezone_get() . "<br />");
echo(date("Y-d-mTG:i:sz",$the_date) . "<br />");
echo(date_default_timezone_set("UTC") . "<br />");
echo(date("Y-d-mTG:i:sz", $the_date) . "<br />");
Here is the simple and tested solution
$presentInst = new \DateTime(now('Asia/Kolkata'), new \DateTimeZone('Asia/Kolkata'));
$present = $presentInst->format("Y-m-d H:i:s e");
echo $present . "<br/>"; // your local time i.e. in IST
$presentInst->setTimezone(new \DateTimeZone("UTC"));
$utcTime = $presentInst->format("Y-m-d H:i:s e");
echo $utcTime; //UTC time
Hope this helps
Please read this, it should solve your problem
http://php.net/manual/en/datetime.gettimezone.php
example :
$given = new DateTime("2014-12-12 14:18:00");
echo $given->format("Y-m-d H:i:s e") . "\n"; // your time
$given->setTimezone(new DateTimeZone("UTC"));
echo $given->format("Y-m-d H:i:s e") . "\n"; // UTC

How to create human readable time stamp?

In my PHP program, I'm using $_SERVER to log the page's date visited:
$dateStamp = $_SERVER['REQUEST_TIME'];
The result is that the $dateStamp variable contains a Unix timestamp like:
1385615749
What's the simplest way to convert it into a human-readable date/time (with year, month, day, hour, minutes, seconds)?
This number is called Unix time. Functions like date() can accept it as the optional second parameter to format it in readable time.
Example:
echo date('Y-m-d H:i:s', $_SERVER['REQUEST_TIME']);
If you omit the second parameter the current value of time() will be used.
echo date('Y-m-d H:i:s');
Your functional approch to convert timestamp into Human Readable format are as following
function convertDateTime($unixTime) {
$dt = new DateTime("#$unixTime");
return $dt->format('Y-m-d H:i:s');
}
$dateVarName = convertDateTime(1385615749);
echo $dateVarName;
Output :-
2013-11-28 05:15:49
Working Demo
<?php
$date = new DateTime();
$dateStamp = $_SERVER['REQUEST_TIME'];
$date->setTimestamp($dateStamp);
echo $date->format('U = Y-m-d H:i:s') . "\n";
?>
you can try this
<?php
$date = date_create();
$dateStamp = $_SERVER['REQUEST_TIME'];
date_timestamp_set($date, $dateStamp);
echo date_format($date, 'U = D-M-Y H:i:s') . "\n";
?>
this code will work for you
$dateStamp = $_SERVER['REQUEST_TIME'];
echo date('d-M-Y H:i:s',strtotime($dateStamp));
REQUEST_TIME - It is unix timestamp - The timestamp of the start of the request.
$dateStamp = $_SERVER['REQUEST_TIME'];
echo date('d m Y', $dateStamp);
OR
$date = new DateTime($dateStamp);
echo $date->format('Y-m-d');

Adding Days to a Date with PHP

I am trying to add a certain amount of days to a set date in PHP. However, all of the code I use is not working. Here is the code I am currently experiencing problems with:
echo date("2013-12-01", strtotime("+7 days"));
I wanting to add 7 days to the date above. When I echo out this code, it just prints '2013-12-01'. Is there a way to do this?
Thanks
For the sake of completeness, here's how you do it with DateTime():
$datetime = new DateTime("2013-12-01");
$datetime->add(new DateInterval('P7D'));
echo $datetime->format('Y-m-d');
or
$datetime = new DateTime("2013-12-01");
$datetime->modify('+7 days');
echo $datetime->format('Y-m-d');
You can use date_add() function:
$date = date_create('2013-12-01');
date_add($date, date_interval_create_from_date_string('7 days'));
echo date_format($date, 'Y-m-d');
This will output 2013-12-08
It must be like this:
$NewDate = date('Y-m-d', strtotime("2013-12-01" . " +7 days"));
echo $NewDate;

Sending time with timezone from PHP to flash

I am trying to send the time to flash but set to the currently timezone. When you view the below even though the echo date, looks like its working the $time is the same. When i test in flash I get the extra hour added. Any help tips welcome on this one...
$format = "d/m/Y H:m:s";
$timezone = "Europe/Amsterdam";
date_default_timezone_set($timezone);
echo "<h1>Timezone ".$timezone."</h1>";
$date = date($format);
echo "<h3>Date: ".$date."<h3>";
$time = strtotime($date);
echo "<h3>Time: ".$time."<h3>";
$date2 = date($format, $time);
echo "<h3>Reverse: ".$date2."<h3>";
$timezone = "Europe/London";
date_default_timezone_set($timezone);
echo "<h1>Timezone ".$timezone."</h1>";
$date = date($format);
echo "<h3>Date: ".$date."<h3>";
$time = strtotime($date);
echo "<h3>Time: ".$time."<h3>";
$date2 = date($format, $time);
echo "<h3>Reverse: ".$date2."<h3>";
?>
Can't you use the PHP time() object for this? Pass this value:
time()."000" // note the trailing zeroes
Call that serverTime and pass it as a query string:
echo "myFlashFile.swf?serverTime=".time()."000";
Then in your Actionscript:
myDate = new Date();
myDate.setTime(serverTime);
Would some sort of mathematics solve the day? ie:
$time = date("U")+date("Z");
This would work for the timezone ahead, but not so good for behind

Categories