Convert database timestamp to date [duplicate] - php

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

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

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 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.

Formatting a date to a specific format [duplicate]

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));

converting datetime in mysql with php [duplicate]

This question already has answers here:
Converting a datetime field timestamp from mysql to php
(2 answers)
Closed 9 years ago.
I have the following DateTime called 'StartTime' field in mysql with '1899-12-30 12:17:52'
I am trying to convert it to a time on the screen for the users with php using
date('h:i:s A',strtotime($row_get_student_times['StartTime']))
but for whatever reason it always returns 4:00 PM no matter what the time. Any clue why this is happening?
It's because you are trying to show a date before the uinx epoch (1970).
Try this:
$date = new DateTime($row_get_student_times['StartTime']);
echo $date->format("h:i:s A");
You need to use DateTime:
$datetime = new DateTime($row_get_student_times['StartTime']);
echo $datetime->format('h:i:s A');
<?php
$dateTimeString = '1899-12-30 12:17:52';
$dateTime = DateTime::createFromFormat('Y-m-d H:i:s', $dateTimeString);
echo $dateTime->format('H:i:s A');

Categories