Formatting a date to a specific format [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I have the following data available : 01/02/2014
What I need to format to is: 2014-01-02 00:00:00
What is the best way to do this in PHP?

$dt = DateTime::createFromFormat('m/d/Y', '01/02/2014');
echo $dt->format('Y-m-d 00:00:00');
See it in action

$val="01/02/2014";
echo date("Y-m-d H:i:s", strtotime($val));
Demo

First of all you should try it yourself.
you should study php date format documentation : http://www.php.net/manual/en/datetime.formats.date.php
Live demo:
https://eval.in/93149
$d = "01/02/2014";
echo date('Y-m-d H:i:s',strtotime($d));
OUTPUT:
2014-01-02 00:00:00

By using PHP date function
$date = "1st April 2000";
OR
$date = "01/04/2000";
echo date("Y-m-d H:i:s",strtotime($date));

Related

Php converting date format not working [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
$date = "31/01/2017";
$date = date("Y-m-d", strtotime($date));
For some the above code is not working and giving me the result: $date = 1970-01-01;
When I am fetching date from database and converting in the same manner its working. But not working when i want to convert "d/m/Y" format to "Y-m-d"
I can convert the date in other ways by exploding, but why not the above code is working..??
try like this
<?php
$date = "31-01-2017";
$date = date("Y-m-d", strtotime($date));
echo $date;
?>
Output:
2017-01-31

Convert database timestamp to date [duplicate]

This question already has answers here:
Formatting an SQL timestamp with PHP
(6 answers)
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a database timestamp with this format 2016-06-01 11:46:00 and I would like to convert and echo it to this format Wed. 01-06-2016.I tried a lot but no desirable results..Any ideas?
Thanks a lot, it worked!
If I want the difference between today and the past date in days or weeks I think I use date_diff.How exactly?
Simply use date and strtotime:
$time = '2016-06-01 11:46:00';
echo date("D. d-m-Y", strtotime($time)); //Wed. 01-06-2016
Updates:
$grk = array("Tet"); // complete the rest of the array
$eng = array("Wed"); // complete the rest of the array
$time = '2016-06-01 11:46:00';
$date = date("D. d-m-Y", strtotime($time));
echo str_replace($eng, $grk, $date);
$time = '2016-06-01 11:46:00';
echo date("D. d-m-Y", strtotime($time)); //Wed. 01-06-2016

How to convert date('H:i A d-M-Y') format in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I want to convert date format in php.
I have a variable $date = '05-27-2016 15:30'. I want to convert it into '3:30 PM 27-May-2016'.
I try this one date('H:i A d-M-Y',strtotime($date)) but it gives wrong result.
How do I do that?
Use createFromFormat function
$date = DateTime::createFromFormat('m-d-Y H:i', '05-27-2016 15:30');
echo $date->format('h:i A d-M-Y'); // 3:30 PM 27-May-2016
As i Commented out both the format, You need to change the format before changing final for final format.
If you need 3 at 03, just use g at h.
General: Online Check
$date = '27-05-2016 15:30';
echo date('h:i A d-M-Y', strtotime($date)); //03:30 PM 27-May-2016
Or ,
using Object Oriented: Online Check
$date = '05-27-2016 15:30';
$date = DateTime::createFromFormat('m-d-Y H:i', $date);
echo $date->format('h:i A d-M-Y');

convert Y/M/d H:i to db format in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have code:
$date= "2016/Apr/16";
$time="5:00 PM";
I want to convert it to DB format like 2016-04-16 17:00:00 in php .
I am not getting exact code from anywhere.
This way will solve your problem.
<?php
$date= "2016/Apr/16";
$time="5:00 PM";
$fullDate = str_replace('/','-',$date) . " " . $time;
$newDate = date("Y-m-d H:i:s", strtotime($fullDate));
echo $newDate;
?>
Result
More Examples;
http://www.w3schools.com/php/func_date_strtotime.asp
Convert date format yyyy-mm-dd => dd-mm-yyyy
Normally you would use the following code:
date('Y-m-d H:i:s', strtotime($date.' '.$time));
If it returns the year 1970, then the input date (the one from $date variable) is not formatted according to a standard that can be recognised by php.

Convert yyyy/mm/dd-hh-mm-ss.00 to Y-m-d H:i:s in php [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I'm developing a system that recibe a date format like: yyyy/mm/dd-hh-mm-ss.00 and I need convert it to Y-m-d H:i:s.
What I done was:
$newDate = date("Y-m-d H:i:s", strtotime($values[2]));
where $values[2] it is the date in that format, for instace: 2014/01/01-13-18-23.00. But the result is: 1970-01-01 01:00:00
Can you help me?
Thanks
You can try with strptime like
$format = "%Y-%m-%d %H:%i:%s";
echo strptime($values[2], $format);
Try using DateTime
$date = DateTime::createFromFormat('Y/m/d-H-i-s.00', '2014/01/01-13-18-23.00');
echo $date->format('Y-m-d H:i:s');
See demo here
You should be able to do something like this:
$date = DateTime::createFromFormat("Y/M/d-H-i-s.00", $values[2]);
echo $date->format("Y-m-d H:i:s");
The dateTime format for strtotime() function are predefined and you have to follow the rules to get desired result.
Mixed date time formats are available here PHP: Compound Formats - Manual

Categories