searls03 Posted January 1, 2013 Share Posted January 1, 2013 is there anyway to compare dates in the format of dd/mm/yyyy? if so, how do I do it? Do i need to convert to a different format? please help Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/ Share on other sites More sharing options...
searls03 Posted January 1, 2013 Author Share Posted January 1, 2013 mm/dd/yyyy format, sorry Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402578 Share on other sites More sharing options...
Jessica Posted January 1, 2013 Share Posted January 1, 2013 Yes. Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402581 Share on other sites More sharing options...
searls03 Posted January 1, 2013 Author Share Posted January 1, 2013 ok.....so could you help me get started? Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402582 Share on other sites More sharing options...
Jessica Posted January 1, 2013 Share Posted January 1, 2013 You ask a vague question, you get a vague answer. Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402583 Share on other sites More sharing options...
searls03 Posted January 1, 2013 Author Share Posted January 1, 2013 I don't know how to describe it well. I want to compare two dates to each other....two in mm/dd/yyyy format. I want to see if date1 is greater that date2. How can I go about with this. Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402584 Share on other sites More sharing options...
Jessica Posted January 1, 2013 Share Posted January 1, 2013 You'll want to use the DateTime library. http://us3.php.net/manual/en/book.datetime.php But mm/dd/yyyy is not a valid format to convert. Where are the dates coming from? Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402585 Share on other sites More sharing options...
searls03 Posted January 1, 2013 Author Share Posted January 1, 2013 I am forming the dates using jquery's datepicker. it shows up as that format and then stores it in a database. I then have a calendar that pulls the date from the database and places it in the calendar. I can't use another format for this, as it is how the calendar is designed to work....believe me, I have tried other formats. I will take a look at the link you provided. I guess maybe I could insert the date into two columns or something and do it that way maybe. Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402586 Share on other sites More sharing options...
searls03 Posted January 1, 2013 Author Share Posted January 1, 2013 what format do I need to put it in in order to compare, and how do I go about comparing? I think I found a code that may be able to convert to different formats, but I need to know what format it needs to be in. Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402587 Share on other sites More sharing options...
Pikachu2000 Posted January 1, 2013 Share Posted January 1, 2013 That's not the right way to store dates in a database. In fact it's a pretty bad way to do it since you lose the ability to run comparisons on them, as you've undoubtedly noticed. Dates should be stored in YYYY-MM-DD format. Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402589 Share on other sites More sharing options...
silkfire Posted January 1, 2013 Share Posted January 1, 2013 Comparing dates is a rather simple task in PHP. In your case I would suggest something like this: $date1 = strtotime($date1); $date2 = strtotime($date2) if ($date2 > $date1) { // CODE } Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402600 Share on other sites More sharing options...
Jessica Posted January 1, 2013 Share Posted January 1, 2013 As was said before, the format hes using is Not a valid format for PHP functions. Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402602 Share on other sites More sharing options...
Pikachu2000 Posted January 1, 2013 Share Posted January 1, 2013 Not to mention that comparing dates in php that are stored in a database is about as efficient as a steam powered light bulb. Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402605 Share on other sites More sharing options...
silkfire Posted January 1, 2013 Share Posted January 1, 2013 I just tried my code with an example date '08/01/2012' and it worked perfectly, Jessica Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402609 Share on other sites More sharing options...
Barand Posted January 2, 2013 Share Posted January 2, 2013 Some formats work better than others <?php echo '<b>UK</b><br />'; echo '25/12/2012 --> ' . date('Y-m-d', strtotime('25/12/2012')) . '<br />'; echo '25-12-2012 --> ' . date('Y-m-d', strtotime('25-12-2012')) . '<br />'; echo '25.12.2012 --> ' . date('Y-m-d', strtotime('25.12.2012')) . '<br />'; echo '<b>US</b><br />'; echo '12/25/2012 --> ' . date('Y-m-d', strtotime('12/25/2012')) . '<br />'; echo '12-25-2012 --> ' . date('Y-m-d', strtotime('12-25-2012')) . '<br />'; echo '12.25.2012 --> ' . date('Y-m-d', strtotime('12.25.2012')) . '<br />'; ?> RESULTS: UK 25/12/2012 --> 1970-01-01 25-12-2012 --> 2012-12-25 ok 25.12.2012 --> 2012-12-25 ok US 12/25/2012 --> 2012-12-25 ok 12-25-2012 --> 1970-01-01 12.25.2012 --> 1970-01-01 Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402674 Share on other sites More sharing options...
salathe Posted January 2, 2013 Share Posted January 2, 2013 But mm/dd/yyyy is not a valid format to convert. Where are the dates coming from? Sure it is, see Date Formats and in particular the "American month, day and year" format. Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402731 Share on other sites More sharing options...
Jessica Posted January 2, 2013 Share Posted January 2, 2013 Yep, I stand corrected, when using the slashes only it works. Sorry about that. I do think it'd be better to fix the JS to use a YYYY-MM-DD format but Op should be able to get it working with strtotime now. Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402754 Share on other sites More sharing options...
salathe Posted January 2, 2013 Share Posted January 2, 2013 I don't know which datepicker the OP is using, but any worth their salt would have a customisable output-date format. I'm with Jessica, in suggesting looking in that direction. Then you can compare the dates either using PHP's DateTime objects or with plain-old-string-comparison. Link to comment https://forums.phpfreaks.com/topic/272579-compare-dates/#findComment-1402757 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.