PHP Display Date Format as UK not US [duplicate] - php

This question already has answers here:
Convert a date format in PHP [duplicate]
(18 answers)
Closed 2 years ago.
In my database I have 10-01-2021 (d/m/Y)
I want to display this on the website as (d F Y) "10 January 2021. However it shows as 1 October 2021
How do I tell the code that the day and the month are not in US order of month/day but actually as day/month?
I have tried using $projectCreated = DateTime::createFromFormat('d/M/Y', $projectCreated); but this doesn't echo anything.
$projectCreated = date_create($projectCreated);
$projectCreated = date_format($projectCreated,"d F Y");

You should echo formatted output instead the static class function.
$date = DateTime::createFromFormat('d-m-Y', '10-01-2021');
echo $date->format('d F Y');

Related

month with leading 0 getting wrong result from createfromformat [duplicate]

This question already has answers here:
DateTime converts wrong when system time is 30 March
(2 answers)
Closed 1 year ago.
I want to convert this date 2021-02 into 02-2021, so I did:
echo DateTime::createFromFormat('Y-m', '2021-02')->format('m/Y');
but this return 03/2021
and the correct result is 02/2021
you have to specify the day:
echo DateTime::createFromFormat('Y-m-d', '2021-02-someday')->format('m/Y');
for example:
echo DateTime::createFromFormat('Y-m-d', '2021-02-01')->format('m/Y');

How to remove separator in date? [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 3 years ago.
how to remove the separator from the date input I have below
$date=$_POST["input"];
$new = date_format($date,"Y M D");
Try this one
<?php
$date=$_POST["input"];
// prints the current time in date format
echo date("Y M D", strtotime($date))."\n";
?>
Use date_create() to change datatype from string to date
$date=date_create($_POST["input"];);
echo date_format($date,"Y M D");
Use strtotime function convert date string into unix timestamp. Then convert into required format.
<?php
echo date("Y M D", strtotime($_POST["input"]));
?>

converting string to date in php not giving exact year [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 5 years ago.
can somebody help me with my problem? I already figure out the problem but seems i do not now how to solve it without using explode(), so the problem is that the string date for example '02-15-17' i want to convert it in date() expected result (Feb 15, 2017) using strtotime() doesn't give me a right/exact year cause of the parameter 3 which is the year it has only 2 digit in HTML5 input tag date in chrome browser, can somebody help me to solve this problem? for the vision of my code here it is.
<?php
$user_date = '02-15-17';
echo date('M d, Y', strtotime($user_date));
?>
result:
Feb 2, 1917
You could also use a DateTime with the format function:
$user_date = '02-15-17';
$dateTime =DateTime::createFromFormat('m-d-y', $user_date);
echo $dateTime->format('M d, Y');
That will give you:
Feb 15, 2017
Output php
Since all your date are over 2000's use substr function try this
$olddate = "02-15-17";
$standarddate = substr($olddate,3,2) . "." . substr($olddate,0,2) . "." . "20" . substr($olddate,6,2);
echo date("jS F, Y", strtotime($standarddate)).' - '.$standarddate;
output : 15th February, 2017 - 15.02.2017

How to get the result of difference between two date using php? [duplicate]

This question already has answers here:
How to get time difference in minutes in PHP
(21 answers)
Closed 5 years ago.
I have this php code
$today_date = date('d/m/Y H:i:s');
$Expierdate = '09/06/2017 21:45:03';
$remaindate = date_diff($today_date,$Expierdate);
echo $remaindate;
and i need result from difference between two date.
date_diff() needs a DateTimeInterface as an argument. In other words, you need to create a DateTime object first, using new DateTime() as shown below.
$today_date = new DateTime();
$Expierdate = new DateTime('09/06/2017 21:45:03');
$remaindate = $today_date->diff($Expierdate);
echo $remaindate->format('%a days');
Live demo
The above would output
90 days
Because today is June 8th, and the format 09/06/2017 is September 6th - because you're using American format (MM/DD/YYYY).
If you ment June 9th (tomorrow), you need to use European format (MM-DD-YYYY, note the dash instead of slash). You can alternatively use DateTime::createFromFormat() to create from a set format, so your current format, 09/06/2017, is interpreted as June 9th. The code would then be
$today_date = new DateTime();
$Expierdate = DateTime::createFromFormat('d/m/Y H:i:s', '09/06/2017 21:45:03');
$remaindate = $today_date->diff($Expierdate);
echo $remaindate->format('%a days');
Output (live demo)
1 days
In any case, $remaindate holds some properties which can be used (see the manual), or you can format it to your liking by supplying the desired formation into the format() method.
new DateTime()
DateTime::diff()
DateTime::format()
DateTime::create_from_format()

Find the Difference between two dates using Datetime::createFromFormat [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 7 years ago.
I'm very new to PHP and Im trying to find the right syntax to get the difference between two dates i got as strings. basically i have two dates;
An estimated completion date ($bcdate)
An actual completion date ($acdate)
A difference of two dates ($rfs)
I've tried using methods posted here and from php.net, but i cant seem to get the output for the difference ($rfs) out.
If i output the bc_date and actual_date alone, it works fine.
Heres what Iv been working with so far:
$bcdate = DateTime::createFromFormat('Ymd', get_field ('bc_date'));
$acdate = DateTime::createFromFormat('Ymd', get_field ('actual_date'));
$rfs = $actual_date->diff($bc_date);
echo $acdate->format('j M Y');
echo $bcdate->format('j M Y');
echo $rfs->format('%R%a days');
$bcdate = DateTime::createFromFormat('Ymd', get_field ('bc_date'));
$acdate = DateTime::createFromFormat('Ymd', get_field ('actual_date'));
$rfs = $acdate->diff($bcdate);
echo $acdate->format('j M Y');
echo $bcdate->format('j M Y');
echo $rfs->format('%R%a days');
Thanks Gunaseelan!
Have you tried using the DateDiff function? http://php.net/manual/en/function.date-diff.php

Categories