Ok, here is my problem. Instead of measuring 7 days in seconds, I want to count how many weeks (Sunday Through Saturday) there are from date #1 to today.
PHP
$today1 = date("Y-m-d");
$diff = strtotime($date1,0) - strtotime($today1,0);
echo (floor($diff / 604800));
If you're counting in seconds, why use date() when you can use time() instead- it gives out the numeric time signature of your current time, making calculations such as this much easier.
Using seconds is fine, perhaps try something like this:
$date1 = "2012-12-25";
$today1 = time();
$diff = strtotime($date1) - $today1;
if($diff < 604800) {
$week = "this week";
} else {
$week = (floor($diff / 604800) == 1)
? floor($diff / 604800) . " week away" : floor($diff / 604800) . " weeks away";
}
echo $week;
Related
I want to implement Birthday countdown time. Normally, birthday normally takes place once in a year eg within 365 days. I downloaded this code from stackoverflow to check the next birthday day of someone who was born on august 22 1982. the days count down should be less than 365 days but the code below is giving a higher value with negative sign.
$rem = strtotime('1982-22-08') - time();
$day = floor($rem / 86400);
$hr = floor(($rem % 86400) / 3600);
$min = floor(($rem % 3600) / 60);
$sec = ($rem % 60);
if($day) echo "$day Days ";
if($hr) echo "$hr Hours ";
if($min) echo "$min Minutes ";
if($sec) echo "$sec Seconds ";
echo "Remaining...";
the next birthday countdown should be less than 365 days
Thanks
Its because you have wrong date format in strtotime and return nothing.
Documentation says
strtotime — Parse about any English textual datetime description into
a Unix timestamp
So if you use
strtotime('08/22/18')
it will work.
Also you should take actual year if you want say "hey your birthday in this is will be..."
What about trying this method:
$birth_month = 8;
$birth_day = 22;
$year = date('Y');
$month = date('n');
$day = date('j');
if ($birth_month < $month || ($birth_month == $month && $birth_day < $day))
$year++;
$target = mktime(0,0,0,$birth_month,$birth_day,$year);
$today = time();
$rem = abs($target - $today);
$days = (int)($rem/86400);
echo "Remaining days till next birthday: $days days!";
I have this problem. Can someone help me,how to convert number of days into the format XX Years, XX Month, XX Days...
i created this function,
function convert($sum) {
$years = ($sum / 365) ;
$years = floor($years);
$month = ($sum % 365) / 30.5;
$month = floor($month);
$days = ($sum % 365) % 30.5; // the rest of days
// Echo all information set
echo 'DAYS RECEIVE : '.$sum.' days<br>';
echo $years.' years - '.$month.' month - '.$days.' days';
}
convert(151);
But with 151 days the result was wrong
DAYS RECEIVE : 151 days0 years - 4 month - 1 days
it must be 4 month ans 28 days not 1 day...
http://sandbox.onlinephpfunctions.com/code/f5e6b4b4f6a27024b66ffbf04e80698722a3ecab
If you use more modern PHP, the following is based around actual days in each month:
$days = 151;
$start_date = new DateTime();
$end_date = (new $start_date)->add(new DateInterval("P{$days}D") );
$dd = date_diff($start_date,$end_date);
echo $dd->y." years ".$dd->m." months ".$dd->d." days";
Note that it will vary, depending on the current date, so you might prefer to set $start_date and $end_date to work from a fixed baseline
$days = 151;
$start_date = new DateTime('1970-01-01');
$end_date = (new DateTime('1970-01-01'))->add(new DateInterval("P{$days}D") );
$dd = date_diff($start_date,$end_date);
echo $dd->y." years ".$dd->m." months ".$dd->d." days";
This is the solution for your problem:
function convert($sum) {
$years = floor($sum / 365);
$months = floor(($sum - ($years * 365))/30.5);
$days = ($sum - ($years * 365) - ($months * 30.5));
echo “Days received: ” . $sum . “ days <br />”;
echo $years . “ years, “ . $months . “months, “ . $days . “days”;
}
//Example data
$current_time = 1318075950;
$unbanned_time = $current_time + strtotime('+1 minute');
if ($unbanned_time > $current_time) {
$th1is = date('Y-m-d H:i:s', $unbanned_time) - date('Y-m-d H:i:s', $current_time);
echo date('Y-m-d H:i:s', $th1is);
I am trying to output how long time it is until the user is unbanned... year months, days, hours, minutes and seconds... But this is giving me some weird results..
You should check manual on how to work with date/time functions.
First of all, instead of
$current_time + strtotime('+1 minute')
use
strtotime('+1 minute', $current_time);
(see manual on strtotime).
Secondly, date function returns a string. Subtracting two strings is not really useful in most cases.
if ($unbanned_time > $current_time) {
$th1is = $unbanned_time - $current_time;
echo $th1is/3600 . ' hours';
}
This will output the remaining time in hours but there are many functions available that will produce better formatting (or you can code one for yourself).
I would recommend to use DateTime
$DateTime = new DateTime();
$unbanned_DateTime = new DateTime();
$unbanned_DateTime = $unbanned_DateTime->modify('+1 minute');
if ( $unbanned_DateTime > $DateTime ) {
$interval = $DateTime->diff($unbanned_DateTime);
$years = $interval->format('%y');
$months = $interval->format('%m');
$days = $interval->format('%d');
$hours = $interval->format('%h');
$minutes = $interval->format('%i');
$seconds = $interval->format('%s');
}
Instead of using every single value as variable you can use ->format() for one output. As you like.
Remember DateTime->format() needs a timezone setting up in your php.ini or with
date_default_timezone_set('....');
date() returns a string, substracting two strings makes no sense here. You can use basic maths to calculate the remaining time:
<?php
$current_time = time();
$unbanned_time = /* whatever */;
$seconds_diff = $unbanned_time - $current_time();
echo "You're unbanned at " . date("Y-m-d H:i:s", $unbanned_time) . " which is over ";
if ($seconds_diff <= 120) {
echo "$seconds_diff seconds";
} else if ($seconds_diff <= 7200) {
echo floor($seconds_diff / 60) . " minutes";
} else if ($seconds_diff <= 7200 * 24) {
echo floor($seconds_diff / 3600) . " hours";
} else {
echo floor($seconds_diff / 3600 / 24) . " days";
}
?>
Hay, i have a database holding events. There are 2 fields 'start' and 'end', these contain timestamps. When an admin enters these dates, they only have the ability to set the day,month,year. So we are only dealing with stamps containing days,months,years, not hours,minutes,seconds (hours,minutes and seconds are set to 0,0,0).
I have an event with the start time as 1262304000 and the end time as 1262908800. These convert to Jan 1 2010 and Jan 8 2010. How would i get all the days between these timestamps? I want to be able to return Jan 2 2010 (1262390400), Jan 3 2010 (1262476800) .. all the way to the end stamp. These events could cross over into different months, say May 28 to June 14.
Any ideas how to do this?
You just have to calculate the number of seconds between the two dates, then divide to get days :
$numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
Then, you can use a for loop to retrieve the dates :
$numDays = abs($smallestTimestamp - $biggestTimestamp)/60/60/24;
for ($i = 1; $i < $numDays; $i++) {
echo date('Y m d', strtotime("+{$i} day", $smallestTimestamp)) . '<br />';
}
Again, if you don't know which timestamp is the smallest, you can use the min() function (second argument in strtotime).
I think that a quick workaround for this is to subtract the amount of a days worth of seconds from the end_stamp until you get to the start_tag.
//1 day = 86400 seconds
I would build an array of the days to use later.
EDIT (example)
$difference = 86400;
$days = array();
while ( $start_time < $end_time )
{
$days[] = date('M j Y', $end_time);
$end_time -= $difference;
}
This should cover any time frame even if its over a bunch of months.
Try this:
while($date_start <= $date_end) {
echo date('M d Y', $date_start) . '<br>';
$date_start = $date_start + 86400;
}
Hope this helps !
$d1=mktime(22,0,0,1,1,2007);
$d2=mktime(0,0,0,1,2,2007);
echo "Hours difference = ".floor(($d2-$d1)/3600) . "<br>";
echo "Minutes difference = ".floor(($d2-$d1)/60) . "<br>";
echo "Seconds difference = " .($d2-$d1). "<br>";
echo "Month difference = ".floor(($d2-$d1)/2628000) . "<br>";
echo "Days difference = ".floor(($d2-$d1)/86400) . "<br>";
echo "Year difference = ".floor(($d2-$d1)/31536000) . "<br>";
http://www.plus2net.com/php_tutorial/date-diff.php
http://www.phpf1.com/tutorial/php-date-difference.html
$daysInBetween = range($startTs, $endTs, 86400);
$secondDay = date('M d Y', $daysInBetween[1]);
/*
$thirdDay = date('M d Y', $daysInBetween[2]);
...
*/
Note that the range() function is inclusive.
**This is a very simple code for find days hours minutes and seconds in php**
$dbDate = strtotime("".$yourbdDate.""); // Database date
$endDate = time(); // current time
$diff = $endDate - $dbDate; /// diffrence
$days = floor($diff/86400); /// number of days
$hours = floor(($diff-$days*86400)/(60 * 60)); //// number of hours
$min = floor(($diff-($days*86400+$hours*3600))/60);///// numbers of minute
$second = $diff - ($days*86400+$hours*3600+$min*60); //// secondes
if($days > 0) echo $days." Days ago";
elseif($hours > 0) echo $hours." Hours ago";
elseif($min > 0) echo $min." Minute ago";
else echo "Just second ago";
Something like this?
$day = $start;
while ($day < $end) {
$day += 86400;
echo $day.' '.date('Y-m-d', $day).PHP_EOL;
}
By the way, 1262304000 is Dec 31, not Jan 1.
get the difference of two dates and divide it by 86400. abs(($date1 - $date2) / 86400) will produce the needed result
I have a table with a datetime field, and I want to pull that time stamp and use PHP to convert it to the nearest largest unit of time. For example, if the entry was made 2 minutes and 36 seconds ago, I want to echo 2 minutes in PHP. If it was 3 hours and 5 minutes ago, I want it to say 3 hours. If it was 6 days and 4 hours and 40 minutes ago, it should say 6 days. You get my drift. And if it's under a minute, just the number of seconds.
I am not familiar at all about any of PHP's date or time functions, so please don't assume I already know anything. Thanks.
Here's another function for you:
function relativeTime($timestamp, $format = "Y-m-d")
{
$difference = time() - $timestamp;
if ($difference >= 604800) { // 7 days
return date($format, $timestamp);
}
$periods = array("Second", "Minute", "Hour", "Day");
$lengths = array("60","60","24","7","4.35","12","10");
$ending = "Ago";
for($j = 0; $difference >= $lengths[$j]; $j++)
$difference /= $lengths[$j];
$difference = round($difference);
if($difference != 1) $periods[$j].= "s";
$text = "$difference $periods[$j] $ending";
return $text;
}
It returns values like "5 Minutes Ago", "3 Days Ago", and "34 Seconds Ago". However if the date is over 7 days old, it just returns the full date, i.e. "2010-04-19".
If your field is date or datetime use
SELECT UNIX_TIMESTAMP(field) FROM...
if your field is stored as an int (seconds since 1970) just use
SELECT field FROM...
Get that integer value from the database into a PHP variable $secs, this is the number of seconds since 1970. We assume the date is in the past, then
$diff = time() - $secs;
if ($diff < 60) echo $diff . ' seconds';
else {
$diff = intval($diff / 60);
if ($diff < 60) echo $diff . ' minutes';
else {
$diff = intval($diff / 60);
if ($diff < 24) echo $diff . ' hours';
else {
$diff = intval($diff / 24);
echo $diff . ' days';
}
}
}