addition of gmt times in php - php

i have a time zone in 2010-05-04T05:27:00.000Z format which indicates the GMT time and i want to add GMT 10+ in to it using php.
i can do that thing using following code but how would i directly add 2010-05-04T05:27:00.000Z and GMT 10+ so that i can get a valid date and time.
$offset=10*60*60;
$dateFormat="d-m-Y H:i::m:s";
echo $timeNdate=gmdate($dateFormat, time()+$offset);

Maybe I'm missing the point but are you not really looking for DateTime::setTimezone?
$timezone = new DateTimeZone('Etc/GMT-10'); // GMT+10:00
$datetime = new DateTime('2010-05-04T05:27:00.000Z');
$datetime->setTimezone($timezone);
echo $datetime->format('r');
// Tue, 04 May 2010 15:27:00 +1000

Use DateTime class http://php.net/manual/en/book.datetime.php exacly DateTime::Add() http://www.php.net/manual/en/datetime.add.php
You have some example here:
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('PT10H30S'));
echo $date->format('Y-m-d H:i:s') . "\n";
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P7Y5M4DT4H3M2S'));
echo $date->format('Y-m-d H:i:s') . "\n";
?>
And another:
<?php
$date = new DateTime('2000-12-31');
$interval = new DateInterval('P1M');
$date->add($interval);
echo $date->format('Y-m-d') . "\n";
$date->add($interval);
echo $date->format('Y-m-d') . "\n";
?>

Related

DateTime gives different results for same negative Date

I have trouble with the following issue.
Environment: PHP 7.0.8
Code example:
$timestamp = -122615337600;
$dt1 = new \DateTime();
$dt1->setTimestamp($timestamp);
$dateStr = $dt1->format('Y-m-d H:i:s');
echo 'DateTime1: ' . $dateStr . '<br>';
$dt2 = new \DateTime($dateStr);
$dateStr = $dt2->format('Y-m-d H:i:s');
echo 'DateTime2: ' . $dateStr . '<br>';
I expect, that the output is equal. But I got:
DateTime1: -1916-06-22 00:00:00
DateTime2: -1916-06-21 00:00:00
Can someone tell me why is this and how can I fix it?
At first sight I would say, thats a PHP bug?

Date Time Comparison Not Working As Expected in PHP even when using a Date Object

I have the following code but its not working as expected even though I am using a DateTime object as suggested by some posts and not to use strtotime()
<?php
$date = new DateTime('september 1st, 2016');
$now = new DateTime("now", new DateTimeZone('America/Phoenix') );
echo 'Date: '. $date->format('Y-m-d H:i:s') . '<br />';
echo 'Now: '. $now->format('Y-m-d H:i:s') . '<br />';
if($date < $now) {
echo '<b>Date is less than now</b>';
}
else
{
echo '<b>Date is greater than now</b>';
}
and the output I get is below, but how is it possible.
Date: 2016-09-01 00:00:00
Now: 2016-08-31 21:24:11
Date is less than now
Check these lines:
$date = new DateTime('september 1st, 2016'); // default timezone
$now = new DateTime("now", new DateTimeZone('America/Phoenix') ); // timezone is set
They both have different time zone of around 3 and half hours that's why the time is different.

Exact date calculator

Is there a way of calculating the EXACT date after/before 6 months in Php taking into consideration the dynamic change in the days of the month i.e. consider some months have 30 days and some have 31 and 28 of course. I know it can be done with MySQL but I want to know if there is an option with Php as well.
Thanks in advance.
you need to date_create, date_interval_create_from_date_string and date_format to complete the job.
$date = date_create('2000-01-01');
date_add($date, date_interval_create_from_date_string('6 months'));
echo date_format($date, 'Y-m-d'); // 2000-07-01
With Object Oriented:
Adding
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P6M'));
echo $date->format('Y-m-d') . "\n"; // 2000-07-01
Subtracting
$date = new DateTime('2000-01-01');
$date->sub(new DateInterval('P6M'));
echo $date->format('Y-m-d') . "\n"; // 1999-07-01
get date before 6 month from current month:
echo date("F 1, Y", strtotime("-6 months"));

Convert timezone in php

I have these timezones. I want to get the current datetime depends on the given timezone. The user will select either one timezone. so need to return current time.
ASKT
CDT
EDT
HST
MDT
MST
PDT
How can i convert? Please help
DateTime::setTimezone would help you.
<?php
$date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));
echo $date->format('Y-m-d H:i:sP') . "\n";
$date->setTimezone(new DateTimeZone('Pacific/Chatham'));
echo $date->format('Y-m-d H:i:sP') . "\n";
?>
Use the DateTime Class
$time = time(); //Get the current time
$date = new DateTime($time, new DateTimeZone('Pacific/Nauru')); //Set a time zone
echo $date->format('Y-m-d H:i:sP') . "\n"; //display date
$date->setTimezone(new DateTimeZone('Europe/London')); //set another timezone
echo $date->format('Y-m-d H:i:sP') . "\n"; //display data again
This, way you don't have to give the same timestamp as new argument every time like mishu's answer.
See the class written by "the_dark_lord12001 at yahoo dot com" this will help you to get timezones from abbreviations & then you can use it
with either date_default_timezone_set or DateTime class
http://www.php.net/manual/en/datetimezone.listabbreviations.php
Hope this help.
~K

Get South Africa Standard Time In Php

this function is used to get current time of my system, I want to get South Africa time, so plz guide me.
$today = time () ;
For example:
<?php
date_default_timezone_set('Africa/Johannesburg');
echo date('Y-m-d H:i:s', time());
$d = new DateTime("now", new DateTimeZone("Africa/Johannesburg"));
echo $d->format("r");
gives
Mon, 07 Jun 2010 02:02:12 +0200
You can change the format. See http://www.php.net/manual/en/function.date.php
time() gives the number of seconds since January 1 1970 00:00:00 GMT (excluding leap seconds), so it doesn't depend on the timezone.
EDIT: For a countdown, you can do:
$tz = new DateTimeZone("Africa/Johannesburg");
$now = new DateTime("now", $tz);
$start = new DateTime("2010-06-11 16:00:00", $tz);
$diff = $start->diff($now);
echo "Days: " . $diff->format("%d") . "\n";
echo "Hours: " . $diff->format("%h") . "\n";
echo "Minutes: " . $diff->format("%i") . "\n";
echo "Seconds: " . $diff->format("%s") . "\n";
The time() function returns the same value all around the world. Its return value is not dependent on the local time zone.
To convert a time value to your local time, call the localtime() function.

Categories