compare two different dates in php [duplicate] - php

This question already has answers here:
How to compare two dates in php [duplicate]
(16 answers)
Closed 3 years ago.
I have 2 dates. One is $monthToCheck = date('Y-m-01'); and the other one comes from an API and its stored in a variable : $reqs['end_date']. My question is : How do i check if the date from the api is bigger than $monthToCheck. With bigger i mean for example 2020 - 01 - 01 is bigger than 2019 - 12 - 01 because the year is higher.
What i tried :
$time = strtotime($reqs['end_date']);
$monthToCheck = date('Y-m-01');
if($time > $monthToCheck) {...}
I converted the date from the API to a date using strtotime and then compared it with my $monthToCheck variable.But i have a feeling something isnt right since the results are all "true" eventho that is not always the case.

Just put your $monthToCheck into strtotime():
$monthToCheck = strtotime(date('Y-m-01'));
Example

php can compare dates formatted year-mm-dd easily
if($monthToCheck > $reqs['end_date']){
// do something
}

Related

Time difference calculate late - PHP [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 years ago.
what I want to do is calculate the latein these time:
Time-in = 22:00 (10:00 PM)
Time-out = 1:30 (1:30 AM)
The output should be 3:30 i want to get this output
How can I calculate the difference and what built-in function in php should be used to achieved this? Can someone give me a clue?
Assuming your times are strings in the form HH:mm, this will work:
$in = new DateTime('22:00');
$out = new DateTime('01:30');
// if in is > out, assume out is on the next day
if ($in > $out) $out->add(new DateInterval('P1D'));
$diff = $out->diff($in);
echo $diff->format('%H:%i');

Work out difference between 2 dates in days [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Closed 4 years ago.
I have two dates. One of them is the current date, one of them is a date somebody uploaded something, this is stored in a database. I need to find out if the date stored in the database is older than 7 days from the current date. I'm using PHP's date(d/m/y);, I've tried some things online, I've tried dateDifference() from php.net, I've tried converting them to timestamps and taking them away, but neither of these seem to work. Is there a simpler way?
This is what carbon is made for. Consider it.
//time in db is in this format 2018-03-16 08:31:09 for this example
$dateInDb = Carbon::createFromFormat("Y-m-d H:i:s",$timeInDb);
$days = Carbon::now()->diffInDays($dateInDb);
Check the library here
try this:
<?php
$upload_date = '09/03/2018'; # d/m/Y format
if (strtotime(date_format(date_create_from_format('d/m/Y',$upload_date),'Y-m-d')) < strtotime('-7 days')) {
echo 'Upload date is older than 7 days ago.';
} else {
echo 'Upload date is not older than 7 days ago.';
}

Convert sql date to DD/MM/YYYY with PHP [duplicate]

This question already has answers here:
Convert a date format in PHP [duplicate]
(18 answers)
Closed 7 years ago.
I've got an SQL server 2000 running with some data.
When I select one of my fields I get something like this:
Sep 21 2015 12:00:00:000AM
Sep 14 2015 12:00:00:000AM
..etc
I would like to convert this thing to "DD/MM/YYYY".
When I'm doing a query with Access I get this format, but not with PHP.
`your query and see time and take it into a variable then fetch that index that comes with time make three array like
`$ar=array();
$ar1=array();
$ar2=array();` after it apply `$ar1[0]=$ar2[0];
$ar1[2]=$ar[1];
$ar1[3]=$ar[0]." ".$ar2[1];
then implode like
$dt=implode("-",$ar1);
$data[$k]['time_from']=$dt;
and print $dt you find your structure.
$timestamp = '31/05/2001 12:22:56';
$timestamp = DateTime::createFromFormat('d/m/Y H:i:s', $timestamp);
echo $timestamp->format('Y-m-d H:i:s');

Current week in webpage [duplicate]

This question already has answers here:
Get week number (in the year) from a date PHP
(18 answers)
Closed 9 years ago.
This is my code , everything works fine except $w which should be present week number .
$w="WEEKOFYEAR()";
I have declared everything and my code works when i hard code the week number
$w=13;
insert($actual,$target['value'],$country,$ini,$goal ,$comment,$an,$w,$con);
I get the following error : Instead of it showing week number 13; it is starting from 0
If you want to get the current week, you may try this
$weekNumber = date("W");
Doc: http://php.net/manual/en/function.date.php
The problem is that WEEKOFYEAR() is not a default PHP function. Use date() to get the present week number.
$w = date('W');
Note that the "W" is a capital W, it makes a difference.
Also, check out the PHP documentation for date(): http://php.net/manual/en/function.date.php

How to find date differences in php [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
PHP calculate person's current age
I want to find the persons age.
$time=$row['date_of_birth'];
$date=explode("-", $time);//gets the date birth from the database and puts it into an array
$a=getdate();//gets todays date
$diff_date= gregoriantojd($a["mon"],a["mday"],a["year"] ) - gregoriantojd(date[1],date[2],date[0]);//subtracts the differences between the dates, this is returned in seconds..
The question is whether this is the way to do it? and how do I convert the seconds and convert them to years (as an integer)?!
EDIT:
I think that the last line should be this:
return floor($diff_date /60*60*24*365);
Just try with strtotime (converts date string into timestamp):
$time = strtotime($row['date_of_birth']);
$age = date('Y', $time - time());
echo 'age: ' . $age;
If $time is supposed to be unix timestamp
$time=$row['date_of_birth'];
$date1 = new DateTime($time);
$date_diff = $date1->diff(new DateTime(), true);
$age = $date_diff->y;
I think you can make use of the date_diff function in PHP.
1st question: The question is whether this is the way to do it?
No. The function gregoriantojd (per the manual entry here) returns a value in days, not seconds. I like the date_diff conversion that Piotr gives the best.
2nd question: how do I convert the seconds and convert them to years
(as an integer)?!
Well, if you were starting with a number of seconds (which you aren't) then your second formula would be mostly correct, but of course it wouldn't account for leap-years. So for anyone older than 4, or at least 8, it would be off for one or more days around their birthday. Usually on their birthday is a pretty important time to get their age correct.

Categories