1

i have a problem with php

$filename = "../ajax_php/5.jpg";
$tanggal = date("d-m-Y", filemtime($filename));
echo $tanggal . "<br/>"; //  26-06-2013

$add_days = 3;
$baru = date('d-m-Y',strtotime($tanggal) + (24*3600*$add_days));
echo $baru . "<br/>"; //  29-06-2013

$skrg = date('d-m-Y');
echo $skrg . "<br/>"; //  07-12-2013

if($baru < $skrg){
    echo "<br/> yes";
}

when i compare 2 date using if $baru < $skrg that should be get output yes, but i fail to get the output

can someone help me?? how to compare 2 date using php??

1
  • 2
    filemtime returns a unix timestamp. you should probably use that format. Alternatively, look into using DateTime PHP class to create date/time objects - these can be compared transparently, and it contains are more reliable inbuilt ways to add/subtract time from a date. Commented Dec 7, 2013 at 8:04

2 Answers 2

2

You cannot compare dates straight away, but you're on the right track with strtotime. What you need to do is compare both dates in Unix time, and then make sure that they aren't the same date but with different seconds.

$baru = strtotime($tanggal) + (24*3600*$add_days);
$skrg = time();
if ($baru < $skrg && date('Y-m-d', $baru) != date('Y-m-d', $skrg)) {
    // do stuff
}
Sign up to request clarification or add additional context in comments.

Comments

2

Try This (only numbers are easy to compare):-


$date1 = strtotime('29-06-2013');
$date2 = strtotime('07-12-2013');

if($date1 < $date2){
    echo "<br/> yes";
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.