compare date between 2 dates and do in function of - php

I am trying to find out how to make this script work?
Basically if the difference between the date stored into the db and today's date is greater then 10 days then the links should be in red if not they should be in black.
<?php
// Today's date
$today = date("d/m/Y");
// The date stored into the DB
$NewsDate = date("d/m/Y", strtotime($getInfo['date']));
// The date stored into the DB + 10 days
$NewsDatePlus10 = date("d/m/Y", strtotime($NewsDate) + (86400 * 10));
if ($NewsDate <= $NewsDatePlus10) {
echo "<span class='list-group-item-heading'><b>". utf8_encode($getInfo['title'])."</b><br /></span>";
echo "<span class='list-group-item-text'>". utf8_encode($getInfo['content'])." <small>". date("d/m/Y", $getInfo['date'])."</small></span>";
} else {
echo "<span style='color:red;'>";
echo "<span class='list-group-item-heading'><b>". utf8_encode($getInfo['title'])."</b><br /></span>";
echo "<span class='list-group-item-text'>". utf8_encode($getInfo['content'])." <small>". date("d/m/Y", $getInfo['date'])."</small></span>";
echo "</span>";
}
?>
in the database the date is stored into an int(11) and it look like this "1465929874" now as you can imagine I did not design the first part I just try to make it do what I need it to do.

I'm unsure as to why you are converting them to d/m/Y format to store in a variable, when you aren't using that variable in any other place than to check.
You should just be able to do:
//Date stored in database
$NewsDate = strtotime($getInfo['date']);
// The date stored into the DB + 10 days
$NewsDatePlus10 = strtotime($NewsDate) + (86400 * 10);
//Now you have a timestamp here.
if ($NewsDate < $NewsDatePlus10) {
//Code here
} else {

Don't do maths with text and you won't need to reinvent the wheel:
$today = new DateTime();
$NewsDate = new DateTimeImmutable($getInfo['date']);
$NewsDatePlus10 = $NewsDate->modify('+10 days');
if ($NewsDate <= $NewsDatePlus10) {
} else {
}
Make sure that $getInfo['date'] comes in an unambiguous format.

Here is your full answer:
<?php
$today = new DateTime();
$NewsDate = date("d/m/Y", strtotime($getInfo['date']));
$NewsDatePlus10 = $NewsDate->modify('+10 days');
if ($NewsDate <= $NewsDatePlus10) {
echo "<span class='list-group-item-heading'><b>". utf8_encode($getInfo['title'])."</b><br /></span>";
echo "<span class='list-group-item-text'>". utf8_encode($getInfo['content'])." <small>". date("d/m/Y", $getInfo['date'])."</small></span>";
} else {
echo "<span style='color:red;'>";
echo "<span class='list-group-item-heading'><b>". utf8_encode($getInfo['title'])."</b><br /></span>";
echo "<span class='list-group-item-text'>". utf8_encode($getInfo['content'])." <small>". date("d/m/Y", $getInfo['date'])."</small></span>";
echo "</span>";
}
?>
But please check your date format when you are retrieving.
When you want to add dates,
For e.g. $NewDate=Date('Y-m-d', strtotime("+3 days"));
For decrease the dates, For e.g. $NewDate=Date('Y-m-d', strtotime("-3 days"));

You can use this example as per your need. this will give you the number of days old you db date is.
$now = time(); // current time
$your_date = strtotime("15-09-2016"); // db date
$datediff = $now - $your_date;
$no_of_days = floor($datediff / (60 * 60 * 24));
if($no_of_days > 10){ echo 'red';}
else { echo 'black';}

Related

Php Stored date from MySQL db convert to equivalent Ethiopian date

I want to convert any Gregorian date to an equivalent Ethiopian Date. The Gregorian date is generated on records created or updated using the PHP date function, date('Y-m-d H:i:s') from the server date. I want to show the changes made are in the Ethiopian date so the views are can easily understand it. I've made a function to change it. but the function date is not reliable in some leap years.
The function returns the date format in either Tigrigna or Amharic language.
Any hint?
function conver_to_ethiopian_date($date, $lang ="ti"){
$OFFSET=79372;
$DAY=86400;
$EYear=1745;
$EMonth=0;
$EDate=0;
$GC=0;
$timezone = 0;
$tot= gmdate($date, 3600*($timezone+date("I")));
$days=round(strtotime($tot)/$DAY);
$days +=$OFFSET;
$mark=0;
$EYear=1745;
while ($mark==0){
if ($EYear % 4 ==3){
if ($days>=366) {
$days-=366;
$EYear=$EYear+1;
}
else{
$mark=1;
}
}
else{
if ($days>=365){
$days-=365;
$EYear=$EYear+1;
}
else {$mark=1;}
}
}
if ($days==0){
$EYear-=1;
//leap year
$EMonth=12;
$EDate=5 + (($EYear%4==3)?1:0);
}
else{
$EMonth = (($days-1) / 30);
if ($days % 30 ==0){ $EDate = 30;}
else {$EDate=$days % 30;}
}
$months_am =array("መስከረም", "ጥቅምት", "ኅዳር", "ታኅሣሥ", "ጥር","የካቲት","መጋቢት","ሚያዝያ", "ግንቦት", "ሰኔ", "ሐምሌ", "ነሐሴ", 'ጳጉሜ');
$months_tg =array("መስከረም", "ጥቅምቲ", "ሕዳር", "ታሕሳስ", "ጥሪ","የካቲት","መጋቢት","ሚያዝያ", "ጉንበት", "ሰነ", "ሓምለ", "ነሓሰ", 'ጳጉሜን');
$days_am =array("እሁድ", "ሰኞ", "ማክሰኞ", "ሮብ", "ሓሙስ", "ኣርብ", "ቀዳሜ");
$days_tg =array("ሰንበት", "ሶኒ", "ሰሉስ", "ረቡዕ", "ሓሙስ", "ዓርቢ", "ቀዳም");
$date_time = explode(' - ', $date);
$year =$year?$year:date("Y") ;
$day_of_week = date("w", mktime(0, 0, 0, $date_time[1], $date_time[2], $date_time[0]));
if ("am" == $lang)
return $days_am[$day_of_week]." ".$months_am[$EMonth]." ".$EDate.", ".$EYear;
else
return $days_tg[$day_of_week]." ".$months_tg[$EMonth]." ". $EDate.", ".$EYear;
}

PHP remove the New text after 1 day

I would add the New text if one day is not passed but if it is passed i would delete the text New if one day is passed.
I tried this code :
<?php
$farkZaman = time() - strtotime("2017-09-26 19:00:00");
$farkGun = floor($farkZaman / (60 * 60 * 24));
if($farkGun < 1){
echo "Yeni";
}
?>
How can i do ?
I need your help.
Note : I have French but i don't have a good English.
Don't know if this is what you are looking for.
Here is set $dt as tomorrow with a time (about 24 hours from now GMT+1).
Then I compare if time is less than this time.
If it is I echo the text.
$dt = strtotime("2017-09-29 06:40");
If(time()<=$dt){
Echo "the text";
}
You can do something like this.
if(strtotime($date_variable) > strtotime("-1 day")) {
echo "New";
}
Try like this
<?php
$now = new DateTime();
$now->format('Y-m-d H:i:s');
$created_date = new DateTime(date('Y-m-d', strtotime("2017-09-27 13:00:00")));
$day = $created_date->diff($now)->days; // 0
if($day < 1){
echo "new texts";
}
?>

how to check if time is more than 30minute?

I have saved a time. I would like to check whether the saved time is 30 minutes greater than the current time. You can see in the code what I have done so far but it doesn't work.
I would need some help fixing that code.
$current = "08:05";
$to_check = date('h:i');
if($to_check > $current + strtotime('30 minute')){
echo "greater";
}
else {
echo "not greater";
}
First, your $current isn't the current time, $to_check is, so your variable names are misleading.
That being said, store your "08:05" as a Unix timestamp then see if the difference between the two is greater than 30 * 60.
$seconds = 1800; // 30 mins
$time1 = strtotime($db_time) + $seconds;
if(time() >= $time1)
echo "Time's up!"
else
echo "time has not expired";
This should work for you:
<?php
$current = date('H:i');
$to_check = date('H:i', strtotime('+30 minutes'));
$to_check = date('H:i', strtotime($record['date'])); //If value is from DB
if($to_check > $current){
echo "greater";
}
else {
echo "not greater";
}
Save time using time function this would give you time in seconds from 1970 so for example current time is 1501137539 and after 30 minutes it would be (1501137539 + 30*60) so you would just need to check if difference b/w current time and stored time is greater that 30*60 then half an hour is over.
Try this snippet:
function x_seconds($to_check = 'YYYY-mm-dd H:i:s') {
$to_check = strtotime($to_check);
$current = time() + 1800; //We add 1800 seconds because it equals to 30 minutes
return ($current>=$to_check) ? true : false;
}
Or go pro, and have a custom "seconds since", just because you can?
function x_seconds($to_check = 'YYYY-mm-dd H:i:s', $seconds = 1800) {
$to_check = strtotime($to_check);
$current = time() + $seconds; //We add x seconds
return ($current>=$to_check) ? true : false;
}
Example usage:
$date = date('Y-m-d H:i:s', '2017-07-27 05:00');
if(x_seconds($date)):
print "Is within 30 minutes.";
else:
print "IS NOT within 30 minutes";
endif;
Try this to solve the issue
$current = strtotime('12:00:00');
$to_check = strtotime(date('H:i:s'));
$newtime = round(( $to_check - $current ) /60 );
if($newtime > 30){
echo "greater";
}else {
echo "not greater";
}

PHP find nearest of two dates

How would I get the nearest of two dates once I've defined them both as variables?
Here's the code to calculate the next Monday and Friday:
//calculates nearest Monday
$nextMonday = strtotime("next Monday");
$mondayParade = date("d/m/Y", $nextMonday);
//calculates nearest Friday
$nextFriday = strtotime("next Friday");
$fridayParade = date("d/m/Y", $nextFriday);
When echoing $mondayParade it displays 26/01/2015 which is correct. $fridayParade also works as above but shows friday's date.
I'd like to be able to calculate which of these two dates is closest to my current date.
I read about also using strtotime for this but can't figure out how.
Thanks
$now = time();
$nextMondayDiff = abs($now - $nextMonday);
$nextFridayDiff = abs($now - $nextFriday);
if ($nextMondayDiff < $nextFridayDiff) {
echo 'Monday is closer';
} else {
echo 'Friday is closer';
}
Or as #David points out in the comments, assuming both dates are always guaranteed to be in the future:
if ($nextMonday < $nextFriday) {
echo 'Monday is closer';
} else {
echo 'Friday is closer';
}
if($nextMonday < $nextFriday) {
echo "Monday";
} else {
echo "Friday";
}

Getting the next date of a day in MySQL database (PHP)

I have a series of weekly events in a database along with the day they happen on (in full form, so: 'Monday', 'Tuesday' etc). I've successfully printed the events in a while loop ordered by today, tomorrow, etc, but I'd like to put the date in brackets next to each one.
I thought it might be a case of (mock code):
$today = date("l");
$todays_date = date("j M");
if (day == $today) {
$date = $todays_date;
}
else if (day == $today + 1) {
$date = $todays_date + 1;
}
else if (day == $today + 2) {
$date = $todays_date + 2;
}
etc...
But I'm not so sure. It'd be ideal if I could just have the date in the database, but this seems to go against the grain of what MySQL is about.
Also, I'd like to ideally format the date as: 11 Jun.
EDIT
Presumably it's also got to fit into my while loop somehow:
if($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_array($result)) {
$items[] = array($row[0]);
echo "<option>" . $row[0] . "</option>";
}
}
You can use strtotime?
echo "Today: ".date("j M");
echo "Tomorrow: ".date("j M", strotime("+1 day"));
You can use strtotime:
echo strtotime("+1 day");

Categories