subtract time. Day is same [duplicate] - php

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Subtract time in PHP
How can i subtract time? the day remains the same. i just need to subtract two time.
Date = 2011-04-26
Starttime = 12:39:53
Endtime = 14:34:28
now i need to calculate the time difference between Endtime and Starttime.
Thanks

Try this one:
<?php
$lasttime = strtotime("-67 minutes"); // for instance
$currentdate = strtotime("now");
$lastact = date("Y-m-d g:i:s a", $lasttime);
$now = date("Y-m-d g:i:s a", $currentdate);
$daycheck = date("d", $now) - date("d", $lastact);
$minutecheck = date("i", $now) - date("i", $lastact);
$difference = $currentdate - $lasttime;
?>

Related

Time deference between two date("F j, Y") dates in php

i have a problem to get month difference between two dates in months.
$d1 = date_create('January 1, 2013');
$date = date("F j, Y");
$d2 = date_create($date);
$dif = date_diff($d1, $d2);
//echo $dif->format('%y years');
echo $dif->format('%m months');
It shows months but not the whole difference in months. I just want the diff in months between dates.
%m only shows up to 11 months. After that years are populated. If you want total months you need to figure in years and do some math:
$d1 = date_create('January 1, 2013');
$d2 = date_create();
$dif = date_diff($d1, $d2);
echo ($dif->format('%m') + $dif->format('%y') * 12) . ' months';
Demo
FYI, the above solution removes unnecessary code. If you want today's date you just don't pass any parameters to date_create().

timestamp check if 10 minutes ago

I have a data in a text file saved using date("Y-m-d h:i:s", strtotime("+2 minutes")) that I need to check if it's 10 minutes ago. I'm trying the following code but it doesn't print anything even if more than 10 minutes ago.
$now = date("Y-m-d h:i:s", strtotime("now"));
if($now > strtotime($old_data))
echo 'expired!';
your comparing a formatted date with a time stamp which explains why nothing works
here:
$now = strtotime("-10 minutes");
if ($now > strtotime($old_data) {
echo 'expired!';
}
You should change either of the following:
$now = strtotime(date("Y-m-d h:i:s", strtotime("now")));
or
if (strtotime($now) > strtotime($old_data))
I'll go for the second. You are comparing a timestamp over date that is why you are not satisfying the condition of if().
Furthermore, you can also use time() if you're only concern is the current timestamp.
$now = time();
or
$now = date("Y-m-d h:i:s", strtotime("now")); // Remove this line
if(time() > strtotime($old_data)) // Change $now with time()

Subtract minutes from date using strtotime

I have the current date format $date = 'yyyy-mm-dd hh:mm' and notice that $date is already set and not using the Date class. I understand I have to use strtotime which I have done to my $date $datestr = strtotime($date). I wish to subtract 5 minutes to the set time.
I've tried this simple thing $A = strtotime($date) - strtotime('-5 min'); But it does not aid me.
its simple to subtract the seconds of 5 minutes which is (5*60)=300 from your time
like this
$time = strtotime($date);
$time_new = $time-(5*60); // will time of the -5 min from the current $time
example
$date = date('d-M-Y g:i:s A'); // current date
echo $date."<br/>"; // output : 17-Feb-2014 8:35:58 AM
$time = strtotime($date); // convert current date to timestamp
$time_new = $time - (5*60); // subtract 5 minutes from current time.
$date_new = date('d-M-Y g:i:s A', $time_new); // convert time to the date again
echo $date_new; // output : 17-Feb-2014 8:30:58 AM

How many days left till any date? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How many days until XXX date?
Currently I am using this code to determine how many days left till an expected day. But this code shows unexpected result. For example if $last_date is 26 December 2012, then I will get 0 day(s) left. But it should be 1 day(s) left. I think my problem is only with floor() function. Isn't it?
$timezone = "Asia/Dhaka";
if(function_exists('date_default_timezone_set')) date_default_timezone_set($timezone);
$now = time();
$last_date = strtotime("$year-$month-$day");
$datediff = $last_date - $now;
$day_left=floor($datediff/(60*60*24));
echo "$day_left day(s) left.";
N:B: My timezone is +6 GMT, I mean Asia/Dhaka.
As per the PHP documentation:
<?php
$year = '2012';
$month = '12';
$day = '26';
$current_date = new DateTime(date('Y-m-d'), new DateTimeZone('Asia/Dhaka'));
$end_date = new DateTime("$year-$month-$day", new DateTimeZone('Asia/Dhaka'));
$interval = $current_date->diff($end_date);
echo $interval->format('%a day(s)');
?>

Display days between one date and today [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find number of days between two dates using php
Days left - subscription expire?
I have code that should echo how many days has been from date (2012-10-01) to today.
$datetime1 = date_create('2012-10-01');
$datetime2 = date_create();
$day1 = date_format($datetime1, 'Y-m-d');
$day2 = date_format($datetime2, 'Y-m-d');
$day = $day2 - $day1 / (60 * 60 * 24);
echo $day;
I cant get any right solution, so any ideas how I should do this?
Check this out Finding the number of days between two dates
Sample code (for your date) below:
$now = time();
$your_date = strtotime('2012-10-01');
$datediff = $now - $your_date;
echo floor($datediff/(60*60*24));

Categories