DateTime from unix timestamp is wrong [duplicate] - php

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
Closed 9 years ago.
I have a string "2013-10-09 00:00:00" and I use the code below to change it to a timestamp
date_default_timezone_set($timeZone);
$timeStamp = strtotime("2013-10-09 00:00:00"); //echos 1381269600
When I do
date_default_timezone_set($timeZone);
date("Y-m-d H:m",$timeStamp);
I get 2013-10-09 00:10:00. This is completely strange. Why do I get this 10 minutes difference?

Because you are using the m which is for the month not for the minute. You need to use i.
See http://php.net/manual/en/function.date.php
Your code should be
date("Y-m-d H:i", $timeStamp);

Related

How would I change one date format to another in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
How would I change the date 18/08/2018 10:46:15 pm to Sat 25, 10:46:15 pm in PHP. I am doing the following:
```
$OldDate = "18/08/2018 10:46:15 pm";
$NewDate = $OldDate->format(D-j, Y-H:I:S);
echo $NewDate;
?>```
However, it is not showing anything, am I formatting it correctly?
Your old date is just a string. If you want to format it, you need to create a DateTime object using that string the constructor.
You're also incorrectly specifying the format
$OldDate = new DateTime("18/08/2018 10:46:15 pm");
echo $OldDate->format("L-j, h:i:s A");
http://php.net/manual/en/class.datetime.php

String to Date or Date and Time [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I have a string like this "20180720171534449" which is a kind of time stamp, is there an easy way I can convert this using PHP and format it as a date or date and time that makes sense to a human?
TIA
Peter
You have an 'YmdHisv' format where v is miliseconds.
Miliseconds is not parsable (as I found out today) with date_create_from_format so you need to remove that first from the string with substr.
$s = "20180720171534449";
$date = date_create_from_format('YmdHis', substr($s,0,-3));
echo date_format($date, 'Y-m-d H:i:s'); //2018-07-20 17:15:34
https://3v4l.org/m1XNd
As Ghost pointed out milliseconds is parasble if using microseconds u instead.
$s = "20180720171534449";
$date = date_create_from_format('YmdHisu', $s);
echo date_format($date, 'Y-m-d H:i:s\.v'); //2018-07-20 17:15:34.449

How to convert SQL datetime date format into unix utc format? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have standart sql datetime format: 2016-10-24 14:26:53.000
And i dont know how to convert into UTC format should be something like: 138853800000
Can be done in sql or php.
I tried something like:
$d = DateTime::createFromFormat('Y-m-d', 2016-10-24, new DateTimeZone('UTC'));
echo $d->getTimestamp().'<br>';
I works but after i add hours minutes it stops
Here is my two cents :
$d = new dateTime("2016-10-24 14:26:53.000", new DateTimeZone("UTC"));
echo $d->getTimestamp();
Output :
1477319213
echo $d->format('Y-m-d H:i:s.u P');
output :
2016-10-24 14:26:53.000000 +00:00

Get the current date and time in PHP with '2016-07-04 00:00:00.000' format [duplicate]

This question already has answers here:
How to get GMT date in yyyy-mm-dd hh:mm:ss in PHP
(6 answers)
Closed 6 years ago.
I am using mssql db with php. Which PHP function can return the current datetime. (i.e.) I want the current date and time to be saved in the following format say for example,
2016-07-04 11:10:05.000
Thanks!
Use date->format http://php.net/manual/it/function.date.php
$date = new DateTime('2000-01-01');
echo $date->format('Y-m-d H:i:s.u');
12-hour format
date("Y-m-d h:i:s.u")
24-hour format
date("Y-m-d H:i:s.u")
With "date" you can format the output and with "time" you get the current unix time.
Example
echo date("Y-m-d H:i:s.u", time());
Documentation: http://php.net/manual/it/function.date.php

convert DateTime to another time zone [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a time in 4:40 pm et, how to convert it in H:i:s, I have used this :
date ('H:i:s' ,strtotime('4:40 pm et'));
You just need to use see timezone and datetime
<?php
$old = date ('H:i:s' ,strtotime('4:40 pm et'));
$date = new DateTime($old);
$newdate = $date->setTimezone(new DateTimeZone('Europe/Moscow')); // +0
print_r($newdate);

Categories