Php converting date format not working [duplicate] - php

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

Related

php convert date format Manual dd/mm/yyyy [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
Use date()
$sqldate = "2016-07-18";
$newDate = date("d-m-Y",$sqldate);
I also use
implode('-', array_reverse(explode('/', $sqldate)));
I am trying to convert a date from yyyy-mm-dd to dd-mm-yyyy (but not in SQL);
however I don't know how the date function requires a timestamp,
and I can't get a timestamp from this string.
I am getting particular date format. WHY? I tried get a date expected format, i tried php date but no success..
I excpected output
18/07/2016
$sqldate = "2016-03-21";
echo date('d/m/Y', strtotime($sqldate));
Use strtotime().
$sqldate = "2016-03-21";
echo $newDate = date("d/m/Y", strtotime($sqldate));
Output
21/03/2016
Live Demo : Click Here
$sqldate = "2016-03-21";
$newDate = date("d-m-Y",strtotime($sqldate));
this is what you need.

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

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 date and time format in passed date value from db [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 6 years ago.
I have a date value from the database and its format is: YYYY-mm-dd hh:mm:ss
I want to convert it to this format: YYYY/mm/dd hh:mm
Here is my code:
<?php
$datetime = $value['last_online']; //from db
$date = date('d-m-Y', $datetime);
$time = date('Gi.s', $datetime);
echo $date.' '.$time;
?>
But it's not working and is giving me an error of:
A non well formed numeric value encountered
I want to get rid of the minus signs and the seconds so my new date and time format would be YYYY/mm/dd hh:mm. How do I do it?
Any ideas?
Use like below
$datetime = $value['last_online']; //from db
$date = date('Y/m/d h:i', strtotime($datetime));

php strtotime is not showing the correct date [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I am trying to convert a date from my database into a different format, but for some reason it's not showing the correct time.
This is the date in the database:
17/06/2015
I'm using this code and its outputing this:
1970-01-01
This is the code I've tried so far, but as you can see it's outputting the incorrect date:
$date = str_replace('/', '-', "17/06/2015");
$date = date('Y-m-d',strtotime("17/06/2015"));
echo $date;
Try this I get the correct result from following code
$date = "17/06/2015";
$repDate = str_replace('/', '-', $date);
$newDate = date("Y-m-d", strtotime($repDate));
echo $newDate;

Categories