Convert mysql query result into different date format [duplicate] - php

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 9 years ago.
I am using the following query to display a timestamp from a database:
<?php echo $testimonial->dated; ?>
this query displays the timestamp as follows: yyyy-mm-dd hh:mm:ss
I tried different date functions in order to display it in the format dd/mm/yyyy but did not have any luck with it, since I dont get how to correctly add this to the query mentioned above. Any idea on how to get this sorted? Thanks so much in advance :)

<?php echo date("d/m/Y", strtotime($testimonial->dated));?>

I didn't understand very well, but:
If this returns a date format (Y-m-d H:i:s) and you want to get the timestamp:
<?php echo strtotime($testimonial->dated); ?>
If this returns a timestamp and you want to get the date format:
<?php echo date('Y-m-d H:i:s', $testimonial->dated); ?>

Related

PHP output timestamp correctly [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 4 years ago.
I am currently outputting a timestamp from my database (code below) but it's in the wrong format.
<p>{$row->last_updated}</p>
It is currently outputting 2018-10-23 13:36:40
The time is ok but the date isn't. This is what I want:
23-10-2018 13:36:40
What do I need to do with this code in order to display it like that?
You can use date()
<p><?php echo date('d-m-Y H:i:s',strtotime($row->last_updated));?></p>
You can change date format using PHP date function like this :
date('d-m-Y H:i:s', strtotime($row->last_updated))

How to convert date from yyyy-mm-dd to dd-mm-yyyy in php? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
I am storing date in yyyy-mm-dd in database. Now i want to display that date in frontend but while displaying date it should be in dd-mm-yyyy format. How to display it in this format please help <?php echo $data2['emp_dob'];?> . I dont want to change my current format of storing date in yyyy-mm-dd. please help me to display it in dd-mm-yyyy format only
date('d-m-Y',strtotime($Your_date));
I don't think it's strictly correct to say that you're storing dates in your MySQL database as yyyy-mm-dd. The internal representation may be something very different than this, but in any case your real question is how to format the date in a certain way. One option is to handle this in your actual MySQL query using DATE_FORMAT(), e.g.
SELECT DATE_FORMAT(date_col, '%d-%m-%Y')
FROM yourTable
use this function, which uses inbuilt function 'substr(str,start,length)'.
function getFormatDate($date){
$date = substr($date,8,2).'-'.substr($date,5,2).'-'.substr($date,0,4);
return $date;
}
getFormatDate($data2['emp_dob']);
if (preg_match("/([0-9]{4})\-([0-9]{2})\-([0-9]{2})/", $data2['emp_dob'], $rg))
$data2['emp_dob'] = $rg[3]."-".$rg[2]."-".$rg[1]
<?php
$ddmmyyyy = date("d-m-Y", strtotime($data2['emp_dob']));
?>
<?php echo date("d-m-Y",strtotime($data2['emp_dob']));?>

PHP Convert Date/Time Format to another [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have a Date/time value as such;
2014-01-07T16:19:08Z
I wish to convert it to the format below using PHP
2015-03-21 02:12:01
I know I could use string replace twice over to remove the T and Z characters but doesn't seem right..
use strtotime() with date()
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
The date_format() function returns a date formatted according to the specified format.
$date=date_create("2014-01-07T16:19:08Z");
echo date_format($date,"Y-m-d H:i:s");
One more way is there
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
Update
The date_create() function returns a new DateTime object.
Reference
<?php
echo date('Y-m-d H:i:s',strtotime('2014-01-07T16:19:08Z'));
?>

PHP Formatting for Dates [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 8 years ago.
I have some issues with some dates coming back from a query in the DD/MM/YYYY format and I'd like to make them appear in the MM/DD/YYYY format.
I had this:
."<td>".$row['date']."</td>\n"
And wanted to use this to format the date:
."<td>".date_format($row['date'],'m/d/y')."</td>\n"
But instead it seems to break it.
Is there any way to format the data the way I want it to appear?
Try this:
$formatted_date= date('m/d/Y',$row['date']);
"<td>".$formatted_date."</td>";
Assuming your query is in PHP do this:
<?
$originalDate = $row['date'];
$newDate = date("m/d/Y", strtotime($originalDate));
echo $newDate;
?>

Time Format : PHP [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Covert time format in php
I am getting a start_time field value from mysql data base as 2012-08-14 21:30:00
and i want to convert it in php format like 2012-08-14T09:30 is there any method to do
this in php ?
use strtotime()
$date = strtotime('2012-08-14 21:30:00');
echo date('Y-m-d\Th:i',$date);
see this example.for the required date & time format:
$date=date("Y-m-d H:i:s");//get the current date
$d=strtotime($date);//convert in strtotime
echo $final_date=date("Y-m-d\Th:i:s",$d);//in the first argument of date ,put the format whatever you want,but be sure to convert it in strtotime first.

Categories