Convert Datetime to String PHP

Convert DateTime to String PHP

You can use the format method of the DateTime class:

$date = new DateTime('2000-01-01');
$result = $date->format('Y-m-d H:i:s');

If format fails for some reason, it will return FALSE. In some applications, it might make sense to handle the failing case:

if ($result) {
echo $result;
} else { // format failed
echo "Unknown Time";
}

DateTime Object to string

Why try and create a new DateTime object use the one you have and just format the output

echo $birthday->format('Y-m-d H:i:s');

Converting a dateTime Object to string

According to the php documentation DateTime does not natively implement the __toString() method, which explains the exceptions you keep getting. You may extend DateTime and implement __toString yourself, provided you are using php 5.2 or later.

class DateTimeExt extends DateTime
{
public function __toString()
{
return $this->format("Y-m-d H:i:s");
}
}

Replace all instances of DateTime used in the bind_param call with DateTimeExt and you should be good.

Convert Date to string using PHP

That is already a string, do you want to change format ?

 $date = '2012/09/26';
echo date('l jS \of F Y', strtotime($date));
// Wednesday 26th of September 2012

How to Convert DateTime to String?

Use DateTime::format().

Php how to convert a date time string in French back to its equivalent in English UTC

You can use IntlDateFormatter to parse dates from strings as well as format them to strings.

In this case, passing a locale of 'fr_FR', and date and time formats of "full" should be enough:

$datetime = 'vendredi 11 février 2022 à 20:19:56 heure normale d’Europe centrale';

$parser = new IntlDateFormatter(
'fr_FR',
IntlDateFormatter::FULL,
IntlDateFormatter::FULL
);
$tsparis = $parser->parse($datetime);

var_dump($tsparis);

Gives int(1644607196); see online demo

This gives you a standard Unix timestamp, which you can process using whatever you want to generate a new output.

convert the api return datetime string from 24 format to 12 hour format in php

You are using AM/PM and 24 hour format. That doesn't make sense. PHP is probably trying its best to make sense of it and adding 12 hours to the datetime pushing it to the next day.

If you ignore the PM from your datetime this works just fine:

$startdate='17/05/2018 23:59:00 PM';
$myDateTime = DateTime::createFromFormat('d/m/Y H:i:s+',$startdate);
$newDateString = $myDateTime->format('d/m/Y h:i:s A');
echo $newDateString;

Demo



Related Topics



Leave a reply



Submit