Work out difference between 2 dates in days [duplicate] - php

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.';
}

Related

Subtracting date in php [duplicate]

This question already has answers here:
Subtracting two dates in php
(7 answers)
Closed 8 months ago.
I'm trying to make a program that subtracts the user's inputted birthdate and the date today.
Here is my code:
$bday = $_POST['bday']; //user input
$today=date("Y-m-d");
$myage= $today - $bday; //i got a warning message here saying "A non-numeric value encountered"
Any idea how to fix this?
Please try this snippet, Assuming you are requiring days and/or years as a result. I used the date_diff function for this code. Converting the user input into date/time( has the Y-m-d format) also I hard coded the user input for easier visualisation.
Working Snippet:
$input="2013-12-12"; #User input
$date = strtotime($input); #Convert user input to date time
$date1=date_create(date("Y-m-d")); #date today
$date2=date_create(date('Y-m-d', $date)); #inputted date
$diff=date_diff($date1,$date2); #date_diff function
echo $diff->format("%Y years or %a days");
Output:
8 years or 3123 days
There is possibly a cleaner version of this. But I believe this is the right flow for your requirements.

PHP strotime - String to Date - wrong date result [duplicate]

This question already has answers here:
Accessing dates in PHP beyond 2038
(5 answers)
Closed 5 years ago.
When I try to convert very high dates, such as 2045-01-01, I get another date:
date("Ymd", strtotime("2045-02-15"));
I obtain a wrong date
19700101
but when
date("Ymd", strtotime("2017-02-15"));
I have the good date
20170215
I don't understand why? Someone just explain to me what's going on?
that's a unixtimestamp problem (=> https://de.wikipedia.org/wiki/Unixzeit#/media/File:Year_2038_problem.gif), better:
date_parse("2006-12-12 10:00:00");
date_parse_from_format ( 'Ymd' , "2017-02-15" );
or
$date = DateTime::createFromFormat('Ymd', "2017-02-15");
echo $date->format('Y-m-d');

PHP - Get timestamp X hours from now [duplicate]

This question already has answers here:
php string in a date format, add 12 hours
(4 answers)
Closed 8 years ago.
I'd like to take a number of hours imputed in a text box and get a timestamp back so I can create a countdown timer.
The countdown is fine so ignore how that is done etc. But whats the best way to get a timestamp '48' hours from now. For example user has entered 48?
$hours = 48;
$timestamp = (new DateTime())->modify("+{$hours} hours")->format('U');
You can create a timestamp like this:
<?php
strtotime(date('d-m-Y H:i:s') . "+ 48 hours");
?>
Your looking for strtotime. For example, you can just use strtotime('+48 hours'), and it will return a unix timestamp for that time.

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