How many days left till any date? [duplicate] - php

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)');
?>

Related

i want convert integer number to date in year in php [duplicate]

This question already has answers here:
PHP: Convert Day of Year to Day of Month and Month
(2 answers)
Closed 1 year ago.
I want to convert the number of year to date
for example in number 76 in 2021 is 17-3
$d = 76;
$x = substr($d, 0, 4) .'-'. substr($d, 4,2) .'-'. substr($d,6,2) .' '. substr($d,8,2) .':'. substr($d,10,2) .':'. substr($d,12,2);
echo $x;
You can use add,
$date = new DateTime("2021");
$days = 76;
$date->add(new DateInterval("P${days}D"));
echo $date->format("Y-m-d H:i:s");
strtotime is the most obvious solution for your task:
$result = date('Y-m-d H:i:s', strtotime('2021-01-00 +76 day'));
The number 76 for the date 17-3 is the day of the year starting with 1. Some solutions with DateTime.
For the current year:
$dayOfYear = 76; //starting from 1
$dateTime = date_create('Jan 1 +'.($dayOfYear-1).' Days');
echo $dateTime->format('Y-m-d H:i:s'); //2021-03-17 00:00:00
Alternatively, the solution from here, changed for the day of the year from 1 and to get the time 00:00.
$dateTime = DateTime::createFromFormat('H:i:sz', "00:00:00".($dayOfYear-1));
If you want to use a fixed year:
$dateTime = date_create('2021-01-01 +'.($dayOfYear-1).' Days');

Date time difference in php [duplicate]

This question already has answers here:
How to calculate the difference between two dates using PHP?
(34 answers)
Converting timestamp to time ago in PHP e.g 1 day ago, 2 days ago...
(32 answers)
Closed 4 years ago.
how can I get difference between 2 time in hours.
For ex:
$data1 = '2018-04-24 02:30:00';
$date2 = now();
how to get diff between $date1 and $date2.
EDIT: code posted by OP in comments
<?php //date_default_timezone_set('UTC+6');
$time1 = strtotime('2018-04-25 12:00:00');
$time2 = time();
echo $time2.'<br>';
echo date('Y-m-d h:i:s', $time2).'<br>';
echo ($time1-$time2)/3600; ?>
<?php
$datetime1 = new DateTime('2018-04-24 02:30:00');
$datetime2 = new DateTime(date('Y-m-d H:i:s'));
$interval = $datetime1->diff($datetime2);
echo $interval->format('%Y %m %d %H:%I:%S');
The result will be:
00 0 1 08:06:15
00 --> years
0 --> months
1 --> days
08 --> hours
06 --> minutes
15 --> seconds
You can modify it as you like but i suggest you keep at least days cause the hours may differ by a few hours but the days can differ by many days.
$data1 = '2018-04-24 02:30:00';
$data2 = date('y-m-d H:i:s');
$formated_in = date('Y-m-d H:i:s', strtotime($data1));
$formated_out = date('Y-m-d H:i:s', strtotime($data2));
$formated_new_in = strtotime($formated_in);
$formated_new_out = strtotime($formated_out);
$sub_total = $formated_new_out - $formated_new_in;
$sub_total = gmdate("H:i", $sub_total);
echo $sub_total;

How to add number of days to exact date format [duplicate]

This question already has answers here:
Adding days to $Date in PHP
(12 answers)
Closed 8 years ago.
Using PHP how i can do this
$today = date("m/d/y"); // showing today
Output 11/18/13 will be saved in mysql as start date
Okay so if i like to add to this start date 30 days ... 60 days ... 90 days or any number of days to be add to $today and results be still in format m/d/y
Example may explain more what i mean
$today = date("m/d/y"); // 11/18/13
then add 10 days so $expired should be $today + 10; in days and results should be 11/28/13
~ thanks
Use DateTime::add() (PHP 5.3)
<?php
$date = new DateTime('2000-01-01');
$date->add(new DateInterval('P10D'));
echo $date->format('Y-m-d') . "\n";
?>
Try something like this should work for you
<?php
$Date = date("m/d/y");
echo date('m/d/y', strtotime($Date. ' + 10 days'));
?>
Use this code:
$today = date("m/d/y");
echo date('m/d/y', strtotime($today. ' +10 day'));
Out put will be 11/28/13 as you desiring.

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));

Calculate number of days remaining [duplicate]

This question already has answers here:
Finding the number of days between two dates
(34 answers)
Closed 3 years ago.
I would like to calculate the number of days remaining before a date. In my database I have a timestamp corresponding to the end date. For example Friday 30. I would like to say something like that :
7 days remaining... 6, 5, 4, etc
Can you help me please ?
$future = strtotime('21 July 2012'); //Future date.
$timefromdb = //source time
$timeleft = $future-$timefromdb;
$daysleft = round((($timeleft/24)/60)/60);
echo $daysleft;
$date1 = new DateTime("2016-01-01"); //current date or any date
$date2 = new DateTime("2016-12-31"); //Future date
$diff = $date2->diff($date1)->format("%a"); //find difference
$days = intval($diff); //rounding days
echo $days;
//it return 365 days omitting current day
$days = round((timestamp_from_database - time()) / 86400);
SELECT DATEDIFF(yourtimestamp, CURDATE()) AS days
doc ref: http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_datediff
$date1=date_create("2013-03-15");
$date2=date_create("2013-12-12");
$diff=date_diff($date1,$date2);
echo $diff->format("%R%a days");
http://php.net/manual/ro/function.date-diff.php

Categories