Convert date in PHP from number to date [duplicate] - php

This question already has answers here:
DateTime->format(epoch) returning the wrong date
(4 answers)
Closed 7 years ago.
How do I convert this date: 1427510741 to: 2015-03-22 09:27:02 in PHP?
I tried: $date2 = new DateTime($date1);
but it gives me: 0741-04-08
And I want this: 2015-03-22 09:27:02

You're close. To use a Unix Timestamp with DateTime to must prepend an # before the timestamp:
$date2 = new DateTime('#'.$date1);
Demo

Related

how to get local date from timestamp = 1574463600 in php mysql [duplicate]

This question already has answers here:
Convert timestamp to readable date/time PHP
(14 answers)
Closed 3 years ago.
i am using sports api and i am getting matches schedule from api but the timestamp is UTC and i want to search match according to my localdate the timestamp is look like this 1574463600
You can use # along with the unix time to create a DateTime class.
<?php
$unixTime = 1574463600;
$date = new DateTime('#' . $unixTime);
echo $date->format('d/m/Y'); // outputs 22/11/2019
See it here https://3v4l.org/eoSU9
Read about DateTime here https://www.php.net/manual/en/class.datetime

how do I read this date-time format 2019-12-17T17:23:49.782Z? [duplicate]

This question already has answers here:
What is this date format? 2011-08-12T20:17:46.384Z
(11 answers)
Closed 3 years ago.
I'm using php and trying to determine what the following date/time format is so I can write PHP code to this date/time format
2019-12-17T17:23:49.782Z
This is iso 8061, so you can try like this:
$date = '2019-12-17T17:23:49.782Z';
$date = date('Y-m-d', strtotime(substr($date,0,10)));
echo $date;

How to change date format dmYhis to Y-m-d in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Convert this string to timestamp PHP [duplicate]
(2 answers)
Convert a date format in PHP [duplicate]
(18 answers)
Closed 5 years ago.
How to change date format dmYhis to Y-m-d in PHP. I have using the date() function but I cannot seem to make it work
$d = '24032017191551';
echo date('Y-m-d', strtotime($d));
Output: 1970-01-01
i want to get the ans is 2017-03-24
Below is the code to change date format
$retrieved ='24032017191551';
$date = DateTime::createFromFormat('dmYHis', $retrieved);
echo $date->format('Y-m-d');

Need to change my date format [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
How can I change the format of my date from 2016-10-05 to 10/05/2016 in php
$date= $row['date'];
So when the row echos out it'll all be the new format
Use DateTime to create an object (from the current format), allowing you to reformat the output.
$date = "2016-10-05";
$date = DateTime::createFromFormat('Y-d-m', $date);
echo $date->format('d/m/Y');
https://repl.it/DqvO

Convert date in format MM/DD/YYYY to MySQL date [duplicate]

This question already has answers here:
Convert from MySQL datetime to another format with PHP
(18 answers)
PHP mysql insert date format
(11 answers)
Closed 9 years ago.
I have a PHP function that gets passed a date in the format MM/DD/YYYY
I need to then convert this so that it can be added to a MySQL field that is of type date
How would I go about doing this in PHP?
$newvalue = date('Y-m-d', strtotime($originalvalue));
MySQL displays the DATE type as 'YYYY-MM-DD', so you could do something like:
date("Y-m-d",strtotime("10/18/2013"));
My variant:
$mysql_date = date('Y-m-d', strtotime(str_replace('/','-', $value)));
$date = preg_replace('/(\d{2})\/(\d{2})\/(\d{4})/', '$3-$1-$2', $date)

Categories