How to format and calculate date based on timezone in php - php

How to format and calculate the date based on timezone .Consider the example below
$date = "2015-12-16 07:00:00";
$timeZonePassed = "CET";
Now my function should return me a date in CET format like mm.dd.yy h.i.s. As i am doing this $dateConv->format($dateFormat) , my date is always displayed as mm/dd/yy but my expectation it should calculate time based on the timezone and display the date format based on timezone.
function formatDate($date, $timeZonePassed) {
$date = date('Y-m-d H:i:s', strtotime($date . "UTC"));
$dateFormat ='m/d/Y h:i A';
$dateConv = date_create_from_format('Y-m-d H:i:s', $date);
$dateConv->setTimezone(new DateTimeZone($timeZonePassed);
return $dateConv->format($dateFormat);
}

try this
function formatDate($date, $timeZonePassed) {
$UTC = new DateTimeZone("UTC");
$date= date('Y-m-d H:i:s', strtotime($date));
$dateConv = new DateTime( $dates, $UTC );
$dateFormat ='m/d/Y h:i A';
$dateConv->setTimezone(new DateTimeZone($timeZonePassed));
return $dateConv->format($dateFormat);
}
work fine this cide useful for you

Related

Calculate time difference for date format ('d/M/y H:i:s A')

I want to calculate time difference between current time and time stored in the db. d/M/y H:i:s A is the format where MS Sql stores the date and time.
The following code returns wrong value.
function time_difference($created_time)
{
$str = $created_time; // 03/Mar/17 12:35:00 PM
$today = date('d/M/y H:i:s A'); // 06/Mar/17 06:35:15 AM
$time_difference = $today - $str; // Returns 3
}
is there anyway to convert $str and $today to seconds?
do it like below:-
<?php
function time_difference($created_time)
{
$str = $created_time; // 03/Mar/17 12:35:00 PM
$today = date('d/M/y H:i:s A'); // 06/Mar/17 06:35:15 AM
return $time_difference = strtotime(str_replace('/','-',$today)) - strtotime(str_replace('/','-',$str));
}
echo time_difference("03/Mar/17 12:35:00 PM");
Output:-https://eval.in/748622
function time_difference($created_time)
{
$created = DateTime::createFromFormat('d/M/y H:i:s A', $created_time);
return strtotime('now') - $created->getTimestamp();
}
<?php
function time_difference($created_time, $date ='',$new_date ='',$time_diff ='')
{
$date = DateTime::createFromFormat('d/M/y H:i:s A', $created_time);
$new_date = $date->format('Y-m-d h:i:s');
$time_diff = strtotime(date('Y-m-d h:i:s')) - strtotime($new_date);
return $time_diff;
}
echo time_difference('03/Mar/17 12:35:00 PM');
?>
Working demo

how to get half DateTime between two dates?

i have current datetime and delivery datetime
$del_time = $this->input->post('bid_time');
date_default_timezone_set('Asia/Kolkata');
$now = date('Y-m-d H:i:s');
i want exact half of datetime in between current datetime and delivery datetime in Y-m-d H:i:s period in php
You can use following code
date_default_timezone_set('Asia/Kolkata');
$del_time = $this->input->post('bid_time'); //for ex '2016-12-01 12:30:00';
$now = date('Y-m-d H:i:s');
$loc_del_date_time = strtotime($del_time);
$loc_curr_date_time = strtotime($now);
$loc_mid_time = ($loc_del_date_time + $loc_curr_date_time) / 2;
$loc_mid_time = round($loc_mid_time);
echo $loc_new_date = date('Y-m-d H:i:s', $loc_mid_time);
Thanks

Cannot convert string into DateTime

I'm trying to convert this date: 25/08/2016 10:45
into DateTime using this:
$time = strtotime('25/08/2016 10:45');
$newformat = date('Y-m-d H:i:s', $time);
but I get false from time, why?
Try this:
$dt = DateTime::createFromFormat('d/m/Y H:i', '25/08/2016 10:45');
echo $dt->format('Y-m-d H:i:s');

Convert the example date format to required format

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

error when echo date() format?

I have a datetime with format: 2012-03-30 11:47:58
$datetime = '2012-03-30 11:47:58';
$publishdate = date('m/d/Y H:i:s', $datetime );
When I echo $publishdate is result is '01/01/1970 07:33:32' , How to fix it
$publishdate = date('m/d/Y H:i:s', strtotime($datetime) );
Or using DateTime
$date = new DateTime($datetime);
$publishdate = $date->format('m/d/Y H:i:s');
http://php.net/manual/en/datetime.format.php
$datetime = '2012-03-30 11:47:58';
$timestamp = strtotime($datetime);
$publishdate = date('m/d/Y H:i:s', $timestamp);
First, convert your date and time to timestamp with strtotime() method, then format again with date() function.
Something like the following should work, note the call to strtotime(), it turns the datestring into a date so you can format it:
$datetime = '2012-03-30 11:47:58';
$publishdate = date('m/d/Y H:i:s', strtotime($datetime) );

Categories