error when echo date() format? - php

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) );

Related

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');

How to change timezones and format time in PHP?

I have a variable like this,
$Timestamp = "Tue Mar 8 15:59:00 UTC-05:00 2016";
How do I change its format to YYYYMM-DD HH:MM AM/PM and change the timezone from UTC to Pacific Time using PHP?
PHP's DateTime object is pretty flexible.
$UTC = new DateTimeZone("UTC");
$TZ = new DateTimeZone("America/New_York");
$date = new DateTime( "2016-03-09 15:00:00", $UTC );
$date->setTimezone( $TZ );
echo $date->format('Y-m-d H:i:s');
Use php date: date('Y/m/d H:i', $timestamp) but your timestamp should be int:
$timestamp = strtotime('22-09-2008');
You can try following
$Timestamp = "Tue Mar 1 15:59:00 UTC-05:00 2016";
$datetime = new DateTime($Timestamp);
$datetime->format('Y-m-d H:i:s') . "\n";
$new_time = new DateTimeZone('Pacific/Apia');
$datetime->setTimezone($new_time);
//New formatted time
echo $datetime->format('Y-m-d H:i:s');

How to format and calculate date based on timezone in 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

How to add mintues to date using php?

I want to add 30 minutes to the current date. But if i do so, it displays 1970-01-01 01:03:33(Unix timestamp).How to retrieve the result in a format that strtotime() parser understands?
Here is my code:
$date1 = date("Y-m-d H:i:s");
$newdate = date("Y-m-d H:i:s", strtotime('+30 minutes', $date1));
That's because date() returns a string, and you are adding 30 minutes to a string instead of a date.
Try this:
$newdate = date("Y-m-d H:i:s", strtotime('+30 minutes', now()));
or
$date1 = date("Y-m-d H:i:s");
$newdate = date("Y-m-d H:i:s", strtotime('+30 minutes', strtotime($date1)));
This should be the answer
echo date("Y/m/d h:i:s", strtotime("+30 minutes"));
strtotime expects a timestamp as its second parameter, not a formatted date. But in any case, you can just work with timestamps:
$newdate = date("Y-m-d H:i:s", time()+30*60);
Try This
$curDate = date('Y-m-d', strtotime("+30 minutes", strtotime(date('Y-m-d'))));
$now = time();
$later = $now + 60*30;

Categories