Convert the example date format to required format - php

I ma getting a response from a Logistic partner like 2013-03-28T13:15:00+05:30 I want this in IST format date. How do i convert it properly.
$addTime = explode('+',$response->TrackDetails->ActualDeliveryTimestamp);
if($addTime[1] == '05:30'){
$time = '';
$time = strtotime('+5:30 hrs',$addTime[0]);
echo date('Y-m-d H:i:s',strtotime($addTime[0]));
}
I tried the above code. but does not work properly.

$addTime = explode('+','2013-03-28T13:15:00+05:30');
list($hours, $minutes) = explode(':', $addTime[1]);
$dt = new DateTime($addTime[0]);
$dt->add(new DateInterval('PT'.intval($hours).'H'));
$dt->add(new DateInterval('PT'.intval($minutes).'M'));
echo $dt->format('Y-m-d H:i:s');
See it in action
EDIT
This is more accurate although the results isn't what you'd expect because of daylight savings time:
$dt = new DateTime('2013-03-28T13:15:00+05:30');
$dt->setTimeZone(new DateTimeZone('UTC'));
echo $dt->format('Y-m-d H:i:s');
See it in action

Related

How to convert string into timezone aware date

Hello I have a string 2020-08-19 13:04:53. I know that this time is in Sydney, Australia time. However, I need to convert this time into New York, America time as a date object. How can I do this?
date_default_timezone_set("Australia/Sydney");
$time = '2020-08-19 13:04:53';
$date = date('Y-m-d H:i:s', $time);
I would try something like
<?php
$date = new DateTime('2020-08-19 13:04:53', new DateTimeZone('Australia/Sydney'));
echo $date->format('Y-m-d H:i:s');
// Output: 2020-08-19 13:04:53
$date->setTimezone(new DateTimeZone('America/New_York'));
echo $date->format('Y-m-d H:i:s');
// Output: 2020-08-18 23:04:53

Convert timezone from name to UTC

Sorry but I still don't know how to write a good title for this question.
In database, I have a string of timezone, ex: "Asia/Bangkok" and I want to convert it to "+07:00". How I can do it?
Here is my code:
$newTZ = new DateTimeZone("Asia/Bangkok");
But I don't know what is next. Thanks you so much.
Simply set the timezone to a DateTime instance and display using the "P" format
$newTZ = new DateTimeZone("Asia/Bangkok");
echo (new DateTime('now', $newTZ))->format('P'); // displays "+07:00" for 'now'
https://3v4l.org/0Cb0C
//GMT
$time = '01/01/2018 12:00 AM';
$zone = 'Asia/Bangkok';
$schedule_date = new DateTime($time, new DateTimeZone($zone) );
$schedule_date->setTimeZone(new DateTimeZone('UTC'));
$time2 = $schedule_date->format('Y-m-d H:i:s');
//Time conveted to TimeZone
echo $time2;
//Compare time between two dates
$date1=date_create($time);
$date2=date_create($time2);
echo "<pre>";
$object = date_diff($date1, $date2);
//Detailed object
print_r($object);
//Get whatever format you want.
echo $object->h . ':' . $object->i;

Adding dates from SQL in PHP (types Datetime + Time)

I'd like to add 2 values from MySQL database in an php app. One is stored as a Datetime type, the other one as Time.
$datetime; // "2013-02-08 14:00:00"
$time; // "01:00:00"
I'd like to add 1 hour to $datetime, this doesn't give me the correct result:
$newdatetime = strtotime($datetime) + strtotime($time);
echo date('Y-m-d H:i:s', $newdatetime); // "1920-02-11 08:31:44"
How can I do this correctly?
$dt = new DateTime("2013-02-08 14:00:00");
$dt->modify('+1 hour');
echo $dt->format('Y-m-d H:i:s');
See it in action
$datetime = DateTime::createFromFormat('Y-m-d H:i:s', $datetime);
// extract H, M, S
$time = sscanf($time, '%d:%d:%d');
// build date interval string
$time = vsprintf('PT%dH%dM%dS', $time);
// add it to your date
$datetime->add(new DateInterval($time));
print $datetime->format('Y-m-d H:i:s');
But if $time is always one hour, John Conde's solution would be a better choice :P
Hmm I've dealt with it this way:
$datetime = "2013-02-08 14:00:00";
$time = "01:00:00"; //not always 1 hour, value from db
$t = explode(":",$time); // array(01,00,00)
$hour = mktime($t['0'],$t['1'],$t['2'],1,1,1970); // 3600
$newdatetime = strtotime($datetime) + $hour;
echo date("Y-m-d H:i:s",$added); // "2013-02-08 15:00:00"
Not the most elegant way, but works! As I'm looking now it's simmilar to One Trick Pony's solution, only not objective.

PHP: add seconds to a date

I have $adate; which contains:
Tue Jan 4 07:59:59 2011
I want to add to this date the following:
$duration=674165; // in seconds
Once the seconds are added I need the result back into date format.
I don't know what I'm doing, but I am getting odd results.
Note: both variables are dynamic. Now they are equal to the values given, but next query they will have different values.
If you are using php 5.3+ you can use a new way to do it.
<?php
$date = new DateTime();
echo $date->getTimestamp(). "<br>";
$date->add(new DateInterval('PT674165S')); // adds 674165 secs
echo $date->getTimestamp();
?>
Just use some nice PHP date/time functions:
$adate="Tue Jan 4 07:59:59 2011";
$duration=674165;
$dateinsec=strtotime($adate);
$newdate=$dateinsec+$duration;
echo date('D M H:i:s Y',$newdate);
Given the fact that $adate is a timestamp (if that's the case), you could do something like this:
$duration = 674165;
$result_date = strtotime(sprintf('+%d seconds', $duration), $adate);
echo date('Y-m-d H:i:s', $result_date);
// add 20 sec to now
$duration = 20;
echo date("Y-m-d H:i:s", strtotime("+$duration sec"));
Do this:
$seconds = 1;
$date_now = "2016-06-02 00:00:00";
echo date("Y-m-d H:i:s", (strtotime(date($date_now)) + $seconds));
$current_time_zone = 150;
date("Y-m-d H:i:s",strtotime(date("Y-m-d H:i:s"))+$current_time_zone);
I made this example for a timezone, but if you change some parts it may help you out:
$seconds_to_add = 30;
$time = new DateTime();
$time->setTimezone(new DateTimeZone('Europe/London'));
$time2 = $time->format("Y/m/d G:i:s");
$time->add(new DateInterval('PT' . $seconds_to_add . 'S'));
$timestamp = $time->format("Y/m/d G:i:s");
echo $timestamp;
echo '========';
echo $time2;
Result:
2018/06/17 3:16:23========2018/06/17 3:15:53
It would be easier with DateTime::modify
(new DateTime($str))->modify("+$duration seconds"); //$str is the date in string
I have trouble with strtotime() to resolve my problem of add dynamic data/time value in the current time
This was my solution:
$expires = 3600; //my dynamic time variable (static representation here)
$date = date_create(date('Y-m-d H:i:s')); //create a date/time variable (with the specified format - create your format, see (1))
echo date_format($date, 'Y-m-d H:i:s')."<br/>"; //shows the date/time variable without add seconds/time
date_add($date, date_interval_create_from_date_string($expires.' seconds')); //add dynamic quantity of seconds to data/time variable
echo date_format($date, 'Y-m-d H:i:s'); //shows the new data/time value
font: https://secure.php.net/manual/en/datetime.add.php (consult Object Oriented style too, the Elzo Valugi solution)
(1) https://secure.php.net/manual/en/function.date.php

Getting the difference between two time/dates using php?

I am wanting to find out the time difference in minutes between two dates which is in the format d-m-Y H:i (14-04-2009 12:15) using php?
Parse the times into timestamps using strtotime() and then simply subtract one from the other.
After that you can get the number of minutes, days and so on by using math functions.
For example:
// $date1 and $date2 are given
// the difference is in seconds
$difference = strtotime($date1) - strtotime($date2);
// getting the difference in minutes
$difference_in_minutes = $difference / 60;
Reference: strtotime()
date_default_timezone_set('Asia/Kolkata');
$currentDateTime = date('m/d/Y H:i:s');
$model_current_time = date('Y-m-d H:i:s',
strtotime($currentDateTime));
echo $model_current_time."------";
$date = DateTime::createFromFormat('d/m/Y h:i:s A',
$row['model_creation_time']);//get from resouses
$new_date_format = $date->format('m/d/Y H:i:s');
$model_creation_time = date('Y-m-d H:i:s',
strtotime($new_date_format));
echo $model_creation_time;
$datetime1 = new DateTime($model_current_time);
$datetime2 = new DateTime($model_creation_time);
$interval = $datetime1->diff($datetime2);
echo $interval->d;
echo $interval->h;
echo $interval->s;

Categories