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

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

Related

PHP Display Date Format as UK not US [duplicate]

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

PHP - Change date on var from array [duplicate]

This question already has answers here:
Convert a date format in PHP [duplicate]
(18 answers)
Closed 4 years ago.
I have an array index that comes from an external API that returns a date in a string like this:
<?php echo gettype($meeting['date']); ?>
Results in "string".
This:
<?php echo $meeting['date']; ?>
Gives the following outcome: "2018-08-30".
I want to change the date format to "30-08-2018".
How can i accomplish this?
You can easily do this using DateTime class and the format() method:
// Create a DateTime instance
$date = new DateTime($meeting['date']);
// Format it:
echo $date->format('d-m-Y');
// Output: 30-08-2018
$newDate = date("d-m-Y", strtotime($meeting['date']));
echo $newDate;
That's done.
$meeting['date']="2018-08-30";
echo date("d-m-Y", strtotime($meeting['date']));
The above code will do the job for you. It will get your string date and convert it to the format you need.
The output of the above code is : 30-08-2018
More about date function you can find here!!!
Use strtotime() and date():
$originalDate = "2018-08-30";
$newDate = date("d-m-Y", strtotime($originalDate));

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

DateTime comparison not working PHP [duplicate]

This question already has answers here:
How to compare two time in PHP
(11 answers)
Closed 6 years ago.
timing comparison not working out if passed dynamically
$date = new DateTime();
$date->setTimezone(new DateTimeZone('America/New_York'));
$myTime =$date->format('H:i');
$myDay =$date->format('l');
if(($s<=$myTime) && ($e>=$myTime||$e=="00:00"))
{$open =1;}
Following works if hard coded
if(("11:30"<="05:39")&&("23:00">="05:39"||"23:00"=="00:00"))
{$open =1;}
where m i going wrong
// your first date
$dateA = '2008-03-01 13:34';
// your second date
$dateB = '2007-04-14 15:23';
if(strtotime($dateA) > strtotime($dateB)){
// something here
}
first of all you are using strings to compare time. Convert to timesstamp using strtotime (http://php.net/manual/en/function.strtotime.php) or mktime(http://php.net/manual/en/function.mktime.php) or use DateTime class (http://php.net/manual/en/class.datetime.php). When comparing strings they are not compared as time, but as character sequences.

Converting date format m.Y into MySQL compatible Y-m-d in PHP [duplicate]

This question already has answers here:
Convert one date format into another in PHP
(17 answers)
Closed 7 years ago.
I have date string like:
08.2015
What I want to do is, to convert it into MySQL compatible date format:
date("Y-m-d", strtotime(date_create_from_format('m.Y', "08.2015")));
Doesn't work as expected. What am I doing wrong?
BTW: It's okay if I'll get 2015-06-01 result.
You don't need strtotime there.
echo DateTime::createFromFormat('m.Y', "08.2015")->format("Y-m-d");
Output: 2015-08-31
Fiddle
And if you always want to get first date of month with only m.Y given, you can do
echo DateTime::createFromFormat('d.m.Y', "01."."08.2015")->format("Y-m-d");
Output: 2015-08-01
Fiddle
<?php
//explode the date
$dateProps = explode('.',"08.2015");
//prepare the format
$date = $dateProps[1].'-'.$dateProps[0].'-01';
//use date functions
$date = date('Y-m-d', strtotime($date));
Try this.
$var = '08.2015';
$date = '01-'.str_replace('.', '-', $var);
echo date('Y-m-d', strtotime($date));
I think below SQL useful to you.
SELECT DATE_FORMAT(STR_TO_DATE('06.2015','%m.%Y'),'%Y-%m');
output: 2015-06
Thank you.
First of all, if you want to use strtotime , it should be this : strtotime('01.08.2015'), that is to say , Year month day must be all needed.
You can try below sql-
SELECT DATE_FORMAT(STR_TO_DATE('08.2015','%m.%Y'),'%Y-%m-01')

Categories