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');
Related
I'm trying to get the date 1 week before at 00:00:00 Here's what I've tried
$now = date("Y-m-d H:i:s");
$start_date_time = date('Y-m-d H:i:s', strtotime("-7 day"));
Output is 2017-04-11 11:33:52 (UTC)
I tried to use
$start_date_time ->setTime(0, 0);
echo $start_date_time ->format('H:i:s');
But it's not datetime so I get an error. Any tips?
you could use strtotime
$start_date_time = date('Y-m-d', strtotime('-7 days'));
or DateTime class
$date = new DateTime('7 days ago');
$date ->format('Y-m-d');
Just use DateTime and DateInterval.
Example:
$date = new DateTime();
$date->sub(new \DateInterval('P7D'));
echo $date->format('Y-m-d 00:00:00');
$d=strtotime("-7 Days");
echo date("Y-m-d 00:00:00", $d);
I am using this code to generate yesterday's beginning of day in PST (aka America/Los_Angeles) time. I can't figure out how to convert the result to UTC.
date_default_timezone_set("America/Los_Angeles");
$time1 = date("Y-m-d H:i:s", mktime(0,0,0, date('n'), date('j')-1, date('Y')));
I tried this, but $time1 is not datetime, it's string. So the following won't work.
$time1->setTimezone(new DateTimeZone("UTC"));
The DateTime class can do all that for you
$date = new DateTime(null, new DateTimeZone('America/Los_Angeles')); // will use now
echo $date->format('d/m/Y H:i:s'); //16/08/2016 16:13:29
$date->setTime(0,0,0);
$date->modify('-1 day');
echo $date->format('d/m/Y H:i:s'); // 15/08/2016 00:00:00
$date->setTimezone(new DateTimeZone('UTC'));
echo $date->format('d/m/Y H:i:s'); // 15/08/2016 07:00:00
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');
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) );
Here is what I have so far:
$date = date('Y-m-d h:i:s', strtotime('-7 days'));
$start = date('Y-m-d h:i:s', strtotime($date,'previous Sunday'));
When outputting $start, it returns: 1969-12-31 06:00:00
What am I doing wrong?
$date needs to e a timestamp
$date = strtotime('-7 days');
$start = date('Y-m-d h:i:s', strtotime('previous Sunday',$date));
You have the arguments the wrong way round:
date('Y-m-d h:i:s', strtotime('previous Sunday', $date));
Edit: Furthermore, you have made $date a formatted string. It needs to be a timestamp, so your code should look something like this:
$date = strtotime('-7 days');
$start = date('Y-m-d h:i:s', strtotime('previous Sunday', $date));
Per php doc
date('Y-m-d h:i:s', strtotime('last Sunday', $date));
If your date is not a timestamp you can still use strtotime, like suppose your date was passed in already and is in a string format of another kind.
$date = '2013-11-10';
$lastsunday = date('Y-m-d',strtotime($date.' last Sunday'));
This can save a bit of time trying to get your date into a format that "works"