I want to calculate the difference in days between two hijri dates, is that possible in Laravel? i tried this code but it is not working:
<?php $diff=date_diff("1438/01/01","1439/01/01"); echo $diff->format("%R%a days"); ?>
EDIT: As I researched more about Hijri Calender, I found that it has only 354 or 355 days.. So you can take of that by subtracting 10 days. I'll be looking for more consistent solution.
Try this.. More about DateTime
<?php
$startDate = new DateTime('1438/01/01');
$endDate = new DateTime('1439/01/01');
echo ($startDate->diff($endDate))->format("%R%a");
In Laravel
use Carbon\Carbon;
then add this in your code.
$startDate = Carbon::parse('1438/01/01);
$endDate = Carbon::parse('1439/01/01);
$dateDiff = $startDate->diff($endDate)->format("%a");
echo $dateDiff;
Related
I'm trying to get a future date from today using PHP.
like date('dS M, Y (D)') returns 21st Jan, 2021 (Thu) (Today)
How can we get a future date from today!
You could use the superb Carbon package by installing it via composer.
Then in your code you can run add days like this
Entering the date piece by piece
$dt = Carbon::create(2021, 1, 22, 0);
echo $dt->addDays(10); // adding 10 days from today on
Using now()
echo Carbon::now()->addDays(10); // adding 10 days from today on
You could use PHP's DateTime class:
$dt = new DateTime();
$dt->modify( '+10 days' );
echo $dt->format( 'dS M, Y (D)' );
https://www.php.net/manual/en/book.datetime.php
You can use it that way. You don't need any package for this job.
$tenDaysLater = date ('Y-m-d', strtotime ('+10 day'));
try this code
$date = date('Y-m-d');
$addDay= strtotime($date . "+10 days");
echo date('Y-m-d', $addDay);
You could do this:
$datetime = Carbon::now()->addDays(30);
I have a date and time saved in a database, in the following format: 2016-04-03 12:54:11
Basically, this date and timestamp represents the exact date and time something was created. What I'm trying to do is display a second date and time, that is the exact number of days since the first timestamp.
So if the timestamp in the dataase was 2016-04-03 12:54:11 and todays date and time is 2016-04-04 12:54:11, it would display: Overdue by: 1 Day
So far I have:
<?PHP $dateCreated = mysql_result(mysql_query("SQL to stored date and time"),0);
$dateNow = time();
$dateDifference = abs(strtotime($dateCreated) - strtotime($dateNow));
$years = floor($dateDifference / (365*60*60*24));
$months = floor(($dateDifference - $years * 365*60*60*24) / (30*60*60*24));
$days = floor(($dateDifference - $years * 365*60*60*24 - $months*30*60*60*24)/ (60*60*24)); ?>
And then:
echo "<strong>Overdue by:</strong> $days Days.";
however, this code displays that 2 dates, only a day apart, as 14 days apart.
Help would be appreciated, cheers.
Try using DateTime objects. They'll make your life much easier.
The above could then be achieved with the following:
$date1 = new DateTime('2016-04-03 12:54:11');
$date2 = new DateTime('2016-04-04 12:54:11');
$diff = $date1->diff($date2);
The $diff variable will then be a DateInterval object, which has a days property to show the amount of days between the two dates. In this case 1, so you can do:
echo "Overdue by: " . $diff->days . " days.";
Which will output Overdue by: 1 days..
On a side-note: You should really not be using mysql_ functions anymore. They have been deprecated for years and are no longer part of the PHP core in the latest PHP version. So this code will not work on an up-to-date server. Also see: Why shouldn't I use mysql_* functions in PHP?
I think the best way to do it is using the Php DateTime class witch allow us to do it.
Try this:
$datetime1 = new DateTime("YOUR_DB_TIMESTAMP");
$datetime2 = new DateTime("today");
$difference = $datetime1->diff($datetime2);
echo difference->d;
Also, you can choose the way that you want to show the diference.
$diff->format('%R%a days')
Reference:
Example-> Finding the number of days between two dates
DateTime Doc -> http://php.net/manual/es/class.datetime.php
Regards!
I would use the DateTime object for things like this, because are really a very big help.
$dateCreatedObj = DateTime::createFromFormat('Y-m-d H:i:s', $dateCreated);
$dateNowObj = new DateTime();
$dateDifference = $dateCreatedObj->diff($dateNowObj);
echo $dateDifference->format('Overdue by: %a Day');
Please note that this does not handle timezones, multilingual and plural issues.
Diff function returns the difference between two DateTimeInterface objects.
You can try to use DateTime object :
<?PHP $dateCreated = mysql_result(mysql_query("SQL to stored date and time"),0);
$dateNow = new DateTime();
$dateOther = new DateTime($dateCreated);
$interval = $dateNow->diff($dateOther);
echo $interval->format('%R%a days');
I am trying to calculate a date difference in days using MySQL and PHP date.
My code
$ArrivalDate = $variants_data['ArrivalDate'];
$daydiff=floor((abs(strtotime(date("d/m/Y")) - strtotime($ArrivalDate))/(60*60*24)));
Output
<td>'.$daydiff.'</td>
Results
I get 93 days instead of 26 days (got 26 days using this calculator http://easycalculation.com/date-day/number-of-days.php)
ArrivalDate value = 2013-05-03 from MySQL table and it changes due to transport delays, etc.
How can I achieve this in PHP?
try this
$daydiff=floor((abs(strtotime(date("Y-m-d")) - strtotime($ArrivalDate))/(60*60*24)));
just change your current date function format so it will give your correct answer means 26 days.
Use DateTime class :
$today = new DateTime;
$oneWeekLater = clone $today;
$oneWeekLater->modify('+1 week');
$diff = $today->diff($oneWeekLater);
echo $diff->format('Y-m-d H:i:s');
http://www.php.net/manual/en/class.datetime.php
http://www.php.net/manual/en/class.dateinterval.php
$diff = strtotime(date("d/m/Y")) - strtotime($ArrivalDate);
echo "Difference is $diff seconds\n";
$days = floor($diff/(3600*24));
echo "Difference is $days days\n";
You can also do it at the database level using the DATEDIFF() function.
http://www.w3schools.com/SQl/func_datediff_mysql.asp
$days = date("d", $timestamp1) - date("d", $timestamp2);
Try this
$ArrivalDate = '2013-05-03';
echo $daydiff=floor((abs(strtotime(date("Y-m-d")) - strtotime($ArrivalDate))/(60*60*24)));
Your date function was not in proper format as that of arrival date
I wrote this piece of code in order to display the current date + 2 months :
<?php
$date = date("d/m/Y");
$date = strtotime(date("d/m/Y", strtotime($date)) . "+2 months");
$date = date("d/m/Y",$date);
echo $date;
?>
It does not seem to work as it displays : 01/03/1970.
What am I doing wrong?
Thanks for your help.
EDIT :
After reading comments and answers, I simplified and corrected it.
<?php
$date = date("d/m/Y", strtotime(" +2 months"));
echo $date;
?>
You're missing the second argument for the second strtotime() call:
echo date('d/m/Y', strtotime('+2 months'));
Try using the DateTime object:
$date = new DateTime("+2 months");
echo $date->format("d/m/Y");
If today is "YYYY-mm-31" and next month does not have the 31th day,
it will show the next month of that day, make the system display "+3 months" result instead of "+2 months" result.
So I guess this is the most safety:
$end_date=date("Y-m-d",strtotime("+2 month",strtotime(date("Y-m-01",strtotime("now") ) )));
Change the date to the 1st day first.
Using DateTime->add() or DateTime->modify()
If you are working with an existing DateTime object, you can use one of these:
// Your date
$date = new DateTime(); // empty for now or pass any date string as param
// Adding
$date->add(new DateInterval('P2M')); // where P2M means "plus 2 months"
// or even easier
$date->modify('+2 months');
// Checking
echo $date->format('Y-m-d');
Beware of adding months in PHP, it may overflow to the next month if the day in the original date is higher than the total number of days in the new month. Same overflow happens with leap years when adding years. Somehow this is not considered a bug by PHP developers and is just documented without a solution. More here:
PHP DateTime::modify adding and subtracting months
I found this to be the most to-the-point solution to address overflow problem:
$day = $date->format('j');
$date->modify('first day of +2 months')->modify('+'. (min($day, $date->format('t')) - 1) .' days');
I've a date like this: 2011-01-28 00:37:15.
What would be the most efficient way to get days from now till this date? I want like number of full days till this date so I could display message: "after X days" rather than showing ugly date.
I am using php 5.2.6.
<?
$date = "2011-01-28 00:37:15";
$date_2 = date("Y-m-d H:i:s");
$date_diff=(strtotime($date)-strtotime($date_2)) / 86400;
?>
Have a look at http://de.php.net/manual/de/datetime.diff.php (PHP >=5.3.0)
This will return you a DateIntervall wich has a public attribute days
<?php
$datetime1 = new DateTime('2011-01-28 00:37:15');
$datetime2 = new DateTime('now');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%d days');
?>
Looks like this should help:
http://www.prettyscripts.com/code/php/php-date-difference-in-days