to find date difference in mongodb query in codeigniter - mongodb

I want to get difference between a date that I found from the database and the other is the current date both are in the same format(example - 2018-06-09 11:02:49).
How can I find the difference between two dates in days format in CodeIgniter?

Try this
$date1 = date_create("2018-06-09 11:02:49");
$date2 = date_create("2018-06-08 11:02:49");
$diff = date_diff($date1, $date2);
echo $diff->format("%a day");

Hope this will help you :
First way : you can use CI date_helper's timespan method
$expired_date = '2018-06-09 11:02:49';
$post_date = strtotime($expired_date);
$now = time();
$units =1;
echo timespan($post_date, $now, $units);
Ouptput :
2 Days
Second way use core php date_diff() method
working demo : https://eval.in/1018721
//$expired_date = $query->row()->expired_at;
$expired_date = '2018-06-09 11:02:49';
$expired_date = date_create($expired_date);
$cdate = date_create('now');
$interval = date_diff($expired_date,$cdate);
//echo $interval->format('%a').PHP_EOL;
echo $interval->d;
Output :
2
CI reference : https://www.codeigniter.com/user_guide/helpers/date_helper.html
For more : http://php.net/manual/en/function.date-diff.php

If you're using PHP 5.3 >, this is by far the most accurate way of calculating the difference:
$earlier = new DateTime("2018-05-20 11:02:49");
$later = new DateTime("2018-06-11 11:02:49");
$diff = $later->diff($earlier)->format("%a day(s)");

Related

Convert epoch difference to number of days

I computed the difference of two ISO 8601 dates after coverting them to epoch.
How can I get the difference of them in number of days?
My code is
my $ResolvedDate = "2014-06-04T10:48:07.124Z";
my $currentDate = "2014-06-04T06:03:36-04:00"
my $resolved_epoch = &convert_time_epoch($ResolvedDate);
my $current_epoch = &convert_time_epoch($currentDate);
if (($resolvedDate - $currentDate) > $noOfDays) {
print "Difference in greater than x\n";
$built = 0;
return ($built);
} else {
print "Difference in smaller than x \n";
$built = 1;
return ($built);
}
sub convert_time_epoch {
my $time_c = str2time(#_);
my #time_l = localtime($time_c);
my $epoch = strftime("%s", #time_l);
return($epoch);
}
Here in addition to $built I also want to return exact number of days, Resolved date is greater than Current date.
"number of days" is awkward, because this is localtime and DST exists (or at least, may exist).
By simply dividing by 86400 you can easily obtain the number of 24-hour periods, which may be sufficient for your purposes.
If you want the true number of times that the mday field has changed, this may be slightly different from the value obtained by this simple division, however.
If the dates are in epoch seconds, take the difference and divide it by the number of seconds in a day (which is 86400). Like so:
my $days_difference = int(($time1 - $time2) / 86400);
If you use DateTime then
my $duration = $dt1->delta_days($dt2); #$dt1 and $dt2 are DateTime objects.
print $duration->days;
use DateTime::Format::ISO8601 qw( );
my $ResolvedDate = "2014-06-04T10:48:07.124Z";
my $currentDate = "2014-06-04T06:03:36-04:00";
my $format = DateTime::Format::ISO8601->new();
my $dt_resolved = $format->parse_datetime($ResolvedDate);
my $dt_current = $format->parse_datetime($currentDate);
my $dur = $dt_resolved->delta_days($dt_current);
my $days = $dur->in_units('days');

Generate a list of the week/year according ISO 8601

I try to generate a list of days with their week number (defined by ISO 8601) accordingly :
mydate='2012-12-25 02:26:55.983'
for (i=1;i<365;i++)
{
mydateAsDate=new Date().parse('yyyy-MM-dd H:mm:ss.S',mydate)+i;
println 'Week ' + mydateAsDate.format('w') + ' => ' + mydateAsDate.format('dd.MM.yyyy');
}
This works but I would like to get the year also like this:
Week 1-2013
I can't figure out which year information I should take.
Any idea?
As Jon Skeet said, I'd recommend using Joda-Time.
If you do, the following should fix your issues:
mydate= new DateTime(2012,12,25)
yearLater = myDate.plusYears(1)
while(myDate < yearLater){
println "Week ${myDate.weekOfWeekyear} - ${myDate.year}"
myDate = myDate.plusDays(1)
}
Not sure I understand, but you mean like:
String startDateString = '2012-12-25 02:26:55.983'
Date startDate = Date.parse( 'yyyy-MM-dd H:mm:ss.S', startDateString )
(1..364).each { i ->
println( (startDate++).format( "dd.MM.yyyy : 'Week' w'-'yyyy" ) )
}
I got it : SimpleDateFormat delivers the right week year information when using the YYYY format
thus this is only available in java 1.7
thanks for your responses though !
cheers

Zend Date doesn't check leap year

I have this code
$dateObject = new Zend_Date('2013-02-28', 'yyyy.mm.dd');
$dateObject->addDay(2);
$newDate = $dateObject->toString('yyyy-mm-dd);
This gives me $newDate = '2013-02-30'; which is not a valid date for database input. Help guys.
Seems that you have given invalid format. Try this,
$dateObject = new Zend_Date('2013-02-28');
$dateObject->addDay(2);
$newDate = $dateObject->toString('Y-MM-dd'); // 2013-03-02

Getting The End Date of the Given Month

I need to get the end date of the given month for some calculation purpose,
how can I do that in PHP, I tried using date() function, but It didn't work.
I used this:
date($year.'-'.$month.'-t');
But this gives the current month's end date.
I think I'm wrong somewhere, I couldn't find where I'm going wrong here.
If I give year as 2012 & month as 03, then it must show me as 2012-03-31.
This code will give you last day for a specific month.
$datetocheck = "2012-03-01";
$lastday = date('t',strtotime($datetocheck));
You want to replace your date() call with:
date('Y-m-t', strtotime($year.'-'.$month.'-01'));
The first parameter to date() is the format you want to be returned, and the second parameter has to be a unix timestamp (or not passed to use the current timestamp). In your case, you can generate a timestamp with the function strtotime(), passing it a date string with the year, the month, and 01 for the day. It will return that same year and month, but the -t in the format will be replaced by the last day of the month.
If you want to return only the last day of the month without year and month:
date('t', strtotime($year.'-'.$month.'-01'));
Just use 't' as your format string.
Current month:
echo date('Y-m-t');
Any month:
echo date('Y-m-t', strtotime("$year-$month-1"));
Try below code.
$m = '03';//
$y = '2012'; //
$first_date = date('Y-m-d',mktime(0, 0, 0, $m , 1, $y));
$last_day = date('t',strtotime($first_date));
$last_date = date('Y-m-d',mktime(0, 0, 0, $m ,$last_day, $y));
function lastday($month = '', $year = '') {
if (empty($month)) {
$month = date('m');
}
if (empty($year)) {
$year = date('Y');
}
$result = strtotime("{$year}-{$month}-01");
$result = strtotime('-1 second', strtotime('+1 month', $result));
return date('Y-m-d', $result);
}
function firstOfMonth() {
return date("Y-m-d", strtotime(date('m').'/01/'.date('Y').' 00:00:00')). 'T00:00:00';}
function lastOfMonth() {
return date("Y-m-d", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00')))). 'T23:59:59';}
$date1 = firstOfMonth();
$date2 = lastOfMonth();
try this ,this give you a current month's starting and ending date.
date("Y-m-d",strtotime("-1 day" ,strtotime("+1 month",strtotime(date("m")."-01-".date("Y")))));
function getEndDate($year, $month)
{
$day = array(1=>31,2=>28,3=>31,4=>30,5=>31,6=>30,7=>31,8=>31,9=>30,10=>31,11=>30,12=>31);
if($year%100 == 0)
{
if($year%400 == 0)
$day[$month] = 29;
}
else if($year%4 == 0)
$day[$month] = 29;
return "{$year}-{$month}-{$day[$month]}";
}
If you are using PHP >= 5.2 I strongly suggest you use the new DateTime object. For example like below:
$a_date = "2012-03-23";
$date = new DateTime($a_date);
$date->modify('last day of this month');
echo $date->format('Y-m-d');

Operation with dates with format ISO 8601?

I need to make an PHP operation with dates with format ISO 8601. Something like:
$starDate = 2012-03-20T00:00:00+01:00; //20 March 2012
$endDate = 2012-04-01T00:00:00+02:00; // 1 April 2012
$diff = $starDate - $endDate; //Result should be: 13
Using this code $diff get a value of cero.
Try this :
function date_diff($date1, $date2)
{
$s = strtotime($date2)-strtotime($date1);
$d = intval($s/86400)+1;
return "$d";
}
Source : http://forum.hardware.fr/hfr/Programmation/PHP/php-calculer-nombre-sujet_30415_1.htm