I am storing the date of a post in mysql database in this form y/m/d I want to check if the post is older than 3 days. Is there any way I can convert it to seconds ? and then check like so:
if($mysql_date > $three_days_old){
echo "old post";
}else {
echo "new post";
}
Using a mix of date() and strtotime() :
$myDate = date('Y-m-d', strtotime($mysql_date));
$oldDate = date('Y-m-d', strtotime('3 days ago'));
if ($myDate > $oldDate) {
echo "old post";
} else {
echo "new post";
}
Try, strtotime, that can parse various time string formats.
You can do following :
$d1 = strtotime(date('Y-m-d', strtotime($mysql_date)));
$d3 = mktime(0, 0, 0, date('m', $d1), date('d', $d1)-3, date('Y', $d1));
$d2 = strtotime(date('Y-m-d', strtotime('3 days ago')));
if ($d3 > $d2) {
echo 'old post';
}else {
echo 'new post';
}
Related
I have a problem with my code, I want to show Tadarus, Kajian, Edukasi and Umum. example I have variable $startdate = '2019-09-20'.
If $startdate + 7day, I want to echo Tadarus
If $startdate + 14day, I want to echo Kajian
If $startdate + 21day, I want to echo Edukasi
If $startdate + 28day, I want to echo Umum
and then if last echo is Umum, I want to show data from Tadarus again. This is my code:
date_default_timezone_set("Asia/Jakarta");
$getdate = date("Y-m-d");
if (strtotime("+7 day", $getdate)) {
echo "Tadarus";
}elseif(strtotime("+14 day", $getdate)){
echo "Kajian";
}elseif(strtotime("+21 day", $getdate)){
echo "Edukasi";
}elseif(strtotime("+28 day", $getdate)){
echo "Umum";
}
Why don't you do something like that:
function echoBasedOnDay($date) {
if ($date === date('Y-m-d', strtotime("+7 days"))) {
echo "Tadarus";
} elseif($date === date('Y-m-d', strtotime("+14 days"))){
echo "Kajian";
} elseif($date === date('Y-m-d', strtotime("+7 days"))){
echo "Edukasi";
} elseif($date === date('Y-m-d', strtotime("+7 days"))){
echo "Umum";
}
}
echoBasedOnDay(date('2019-09-27')); // You can pass any date you want in here as the function's parameter
I want to check a certain date that is stored in my DB, if this date is during this fiscal year it prints valid if it is not it prints invalid
here is my php code:
$init_date= date("2016/07/01");
$end_date= date("2017/06/30");
$i_date = strtotime($init_date);
$e_date = strtotime($end_date);
$date_db= strtotime($date);// this $date is retrieved from my DB
if ($e_date > $date_db && $i_date < $date_db)
{ echo "valid";}
else { echo "invalid";}
but the problem is that i don't want to set the start and the end dates manually, is there a way to make it dynamic? as it will be updated every year
This way your script will be a bit more dynamic.
// the below snippet checks the date retrieved from database
// against fiscal periods between years 2000 and 2050 and return the
// valid dates
$endYear=2000;
while($endYear<=2050) {
$end = $endYear.'/06/30';
$endDate = DateTime::createFromFormat('Y/m/d', $end);
$initDate = DateTime::createFromFormat('Y/m/d', $end);
$initDate = $initDate->sub(new DateInterval('P1Y'))->add(new DateInterval('P1D'));
$ddb = '2016-09-27';
$dateFrodDB = DateTime::createFromFormat('Y-m-d', $ddb);
if ($dateFrodDB>=$initDate && $dateFrodDB<=$endDate)
{ echo "valid\n";
echo "\tStartDate->\"".$initDate->format("Y-m-d")."\"\n";
echo "\tEndDate->\"".$endDate->format("Y-m-d")."\"\n";
echo "\tDateFromDatabase->\"".$dateFrodDB->format("Y-m-d")."\"\n";
}
$endYear++;
}
/* output
valid
StartDate->"2016-07-01"
EndDate->"2017-06-30"
DateFromDatabase->"2016-09-27"
*/
check this on PHP Sandbox
Try this,
<?php
$start_date = date('Y-m-d', strtotime(str_replace("/", "-", $initdate)));
$end_date = date('Y-m-d', strtotime(str_replace("/", "-", $end_date)));
$new_date = date('Y-m-d', strtotime(str_replace("/", "-", $date)));
if (($start_date < $new_date && $new_date < $end_date) || ($end_date < $new_date && $new_date < $start_date)) {
echo "valid";
} else {
echo 'invalid';
}
Here is working link
i am trying to get number of days between two given dates, but while trying this way its not giving the number of days.
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$diff = date_diff($todate,$pur_dt);
print_r($diff);
echo $diff->format('%R%a days');
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
its not working, not giving the number of days between given two dates.
Try this. It is very simple.
<?php
$date1 = strtotime("2015-11-16 10:01:13");
$date2 = strtotime("2015-05-06 09:47:16");
$datediff = $date1 - $date2;
echo floor($datediff/(60*60*24))." days"; //output 194 days
?>
It's better using DateTime class, you can see comment(9) at PHP manual as it answer your question
Try this,
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$datediff = $pur_dt - $todate;
$diff = $datediff/(60*60*24);
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
Try This :
$pur_dt = Date('2015-08-03');
$todate = Date(date('Y-m-d'));
$pur_dt = strtotime($pur_dt);
$todate = strtotime($todate);
$seconds_diff = $todate - $pur_dt;
$$diff = floor($seconds_diff/(60*60*24));
if($diff>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
Try this
$pur_dt = date_create('2015-08-03');
$todate = date_create(date('Y-m-d'));
$diff = date_diff($todate,$pur_dt);
print_r($diff);
echo $diff->format('%R%a days');
if($diff->days>15) //checking condition if $pur_dt - $todate > 15
{
echo 'Hello you are not eligible';
}
else
{
echo 'eligible';
}
I want to check whether the current year is greater than a date string(D-M-Y) here is my code
$OldDate = "09-30-2011";
$OldYear = strtok($OldDate, '-');
$NewYear = date("Y");
if ($OldYear < $NewYear) {
echo "Year is less than current year"
} else {
echo "Year is greater than current year";
}
You can use strtotime():
$OldDate = "2011-09-30";
$oldDateUnix = strtotime($OldDate);
if(date("Y", $oldDateUnix) < date("Y")) {
echo "Year is less than current year";
} else {
echo "Year is greater than current year";
}
UPDATE
Because you're using an unconventional datestamp, you have to use different methods, eg:
$OldDate = "09-30-2011";
list($month, $day, $year) = explode("-", $OldDate);
$oldDateUnix = strtotime($year . "-" . $month . "-" . $day);
if(date("Y", $oldDateUnix) < date("Y")) {
echo "Year is less than current year";
} else {
echo "Year is greater than current year";
}
NOTE: If you want to always be sure that your date gets correctly understood by strtotime, use YYYY-MM-DD
use the date function to get year
$OldDate = date("Y",strtotime("09-30-2011"));
$NewYear = date("Y",strtotime("now"));
if($OldYear<$NewYear)
{
echo "Year is less than current year"
}
else
{
echo "Year is greater than current year";
}
You can achieve your goal by doing:
$input_date = date("09-30-2011");
$input_date_arr = explode("-", $input_date);
$currYear = date("Y");
$inputYear = $input_date_arr[2];
if ($currYear > $inputYear) {
echo "Current year is greater than given year!";
} else {
echo "Current year is not greater than given year!";
}
$OldDate = "09-30-2011";
$OldYear = date('Y',strtotime($OldDate));
$NewYear = date("Y");
if($OldYear<$NewYear)
{
echo "Year is less than current year"
}
else
{
echo "Year is greater than current year";
}
You could convert the string to a timestamp, and check it with the current timestamp
if(time($OldDate) < time()){
// do stuff
} else {
// do other stuff
}
do this,
$dateString = '2021-02-24';
$yr = date("Y", strtotime($dateString));
$mon = date("m", strtotime($dateString));
$date = date("d", strtotime($dateString));
I would like to know how to check if the timestamp is today, tomorrow or the day after tomorrow.
I have e.g. :
$timestamp = "1313000474";
How to make a check if this timestamp is today,tomorrow or the day after tomorrow?
e.g.
if today then echo $output = "today";
if tomorrow then echo $output = "tomorrow";
if the day after tomorrow then echo $output = "dayaftertomorrow";
How to do this?
EDIT: corrected unix timestamp
Thank you in advance.
$timestamp = "1313000474";
$date = date('Y-m-d', $timestamp);
$today = date('Y-m-d');
$tomorrow = date('Y-m-d', strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', strtotime('tomorrow + 1 day'));
if ($date == $today) {
echo "today";
} else if ($date == $tomorrow) {
echo "tomorrow";
} else if ($date == $day_after_tomorrow) {
echo "dayaftertomorrow";
}
Keep your code clean...
$timestamp = "1313000474";
// Description demonstrate proposes only...
$compare_dates = array(
'today' => 'today!!',
'tomorrow' => 'Tomorrow!!!',
'tomorrow +1 day' => 'day after tomorrow? YEAH',
);
foreach($compare_dates => $compare_date => $compare_date_desc){
if(strtotime($compare_date) > $timestamp && $timestamp < strtotime($compare_date.' +1 day') ){
echo $compare_date_desc;
break;
}
}
EDIT: With this you dont have to worry if the timestamp is already without hours, minutes and seconds... Or create different output dates, replacing echo $compare_date_desc; by echo date($compare_date_desc,$timestamp);
<?php
$time = "20060713174545";
$date = date('Y-m-d', strtotime($time));
$now = date('Y-m-d');
$tomorrow = date('Y-m-d', time() + strtotime('tomorrow'));
$day_after_tomorrow = date('Y-m-d', time() + strtotime('tomorrow + 1 day'));
if ($date == $now){
echo "It's today";
}
elseif($date == $tomorrow){
echo "It's tomorrow";
}
elseif($date == $day_after_tomorrow){
echo "It's day after tomorrow";
}
else{
echo "None of previous if statements passed";
}
<?php
function getTheDay($date)
{
$curr_date=strtotime(date("Y-m-d H:i:s"));
$the_date=strtotime($date);
$diff=floor(($curr_date-$the_date)/(60*60*24));
switch($diff)
{
case 0:
return "Today";
break;
case 1:
return "Yesterday";
break;
default:
return $diff." Days ago";
}
}
?>