Convert DateTime class to string [duplicate] - php

This question already has answers here:
Object of class DateTime could not be converted to string
(8 answers)
Closed 9 years ago.
Currently I am retrieving a date from mssql with the data type of small date time.
The data is : 2013-03-12 00:00:00
I want to store it in a variable and then display on a text box.
And the format I want to display is just 2013-03-12 on the text box.
The message I get is:
catchable fatal error : Object of class DateTime could not be
converted to string.
Any idea?

in php you can simply use date_format($date, 'Y-m-d')
<?php
$date = date_create('2013-11-23 05:06:07');
echo date_format($date, 'Y-m-d');
?>
returns
2013-11-23

The following code will give you a date usable for SQL:
$dateTime->format(\DateTime::ISO8601);
-edit-
Actually, I just saw that you want it the other way around. In that case:
$dateTime->format('Y-m-d');

Use CONVERT:
SELECT CONVERT(VARCHAR(10), datefield, 121)
FROM tablename;

Related

How do you format date as the following example: 2021-08-17T10:19:02.019Z in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 12 months ago.
I have use PHP to convert a simple datetime from MySQL
example: 2022-02-21 10:26:27
to a format like this one:
2021-08-17T10:19:02.019Z.
What function and formatting pattern should I use?
Also, could you please help me understand what the .019Z stand for?
Thank you!
EDIT:
Using echo date("c"); results in an output like this one: 2022-02-21T14:14:22+01:00, which differs form the desired output in the latest part (milliseconds and timezone).
On the other hand, if I do echo date("Y-m-d\TH:i:s.v"); I get an output like the following: 2022-02-21T14:22:24.000, where I should only be missing the timezone. I am not able to find it in the documentation.
The server isn't on the standard timezone, and I would like not to change it. Is there a way to get it dynamically?
The format you're looking for is ISO8601.
You can use the defined constants in DateTime object in PHP:
echo $objDateTime->format('c');
echo $objDateTime->format(DateTime::ISO8601);
See also DateTime formats in PHP documentation and the DateTime class itself
DateTime::format -- DateTimeImmutable::format -- DateTimeInterface::format -- date_format — Returns date formatted according to given format
https://www.php.net/manual/en/datetime.format.php

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

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

Convert mysql query result into different date format [duplicate]

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

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