close ad
 
Important WebAssist Announcement
open ad
View Menu

Technical Support Forums

Free, outstanding support from WebAssist and your colleagues

rating

Compare MYSQL DATETIME function in PHP

Thread began 9/22/2010 7:22 am by John Langer | Last modified 9/22/2010 9:59 am by Jason Byrnes | 5740 views | 9 replies |

John LangerBeta Tester

Compare MYSQL DATETIME function in PHP

In the MySql database I have two columns, start_date and end_date they are both DATETIME functions so they store both the date and the time

In the sites page I'm displaying the start date, the start time and end time + plus the end date. BUT what I want to do is only show the end date when it's different from the start date like this

Date: 24 Sep 2010

Time: 7:30 pm - 10:00 pm (this being a one day event)

OR

Date: 24 Sep 2010 - 29 Sep 2010

Time: 7:30 pm - 10:00 pm (this being a multi-day event)

My current code is:

php:
Date: <?php echo date('j M Y',strtotime ($row_rsCiviEventResults['start_date'])); ?> 
    <?php if ($row_rsCiviEventResults['end_date']!=""){ ?> 
      - <?php echo date('j M Y',strtotime ($row_rsCiviEventResults['end_date'])); ?> 
      <?php ?>

So if the end_ date datetime is empty it doesn't show it. However if I add a second criteria of saying "OR if the end_date is not the same at the start_date, then don't show it either"

php:
Date: <?php echo date('j M Y',strtotime ($row_rsCiviEventResults['start_date'])); ?> 
    <?php if ($row_rsCiviEventResults['end_date']!="" ||  $row_rsCiviEventResults['end_date'] !=  $row_rsCiviEventResults['start_date'] ){ ?> 
      - <?php echo date('j M Y',strtotime ($row_rsCiviEventResults['end_date'])); ?> 
      <?php ?>

Now this didn't work but then I realised that the end_date would always differ from the start_date because it also stores the time. "2010-09-01 09:00:00" compared to "2010-09-01 15:00:00" for example.

So that was a long winded way of asking, how do I compare just the date and not the time part of the DATETIME function in the PHP code.

I should add that this MySQL database is part of a third party application called civiCRM so I am unable to alter the database.

The page in question is here. In this case the second date should show as the event runs for more than one day but on this page it should NOT show as the date is identical (but the time is not of course).

Sign in to reply to this post

Jason ByrnesWebAssist

use date formatting in the if statement, the same way you are changing the format to display it on the page:

php:
<?php if ($row_rsCiviEventResults['end_date']!="" ||  date("j m Y"strtotime($row_rsCiviEventResults['end_date'])) !=  date(j m Y", strtotime($row_rsCiviEventResults['start_date'])) ){ ?>
Sign in to reply to this post

John LangerBeta Tester

Thanks Jason. It looks like I should have known that! :)

Haven't tried it yet but I will soon and report back.

Sign in to reply to this post

Jason ByrnesWebAssist

no worries.

Sign in to reply to this post

John LangerBeta Tester

No, doesn't work :(

It occurred to me that the code should refer to how the date is set out in the database "Y-m-d" so I altered that but it still doesn't work.

This is the code that I now have on the site

php:
<?php if ($row_rsCiviEventResults['end_date']!="" || date("Y-m-d"strtotime($row_rsCiviEventResults['end_date'])) !=  date("Y-m-d"strtotime($row_rsCiviEventResults['start_date'])) ){ ?>

I'm sure it must be something simple that I'm missing here.

Sign in to reply to this post

Jason ByrnesWebAssist

that doesn't make any sense.


try echoing whats being compared:

php:
<?php echo("start date: ".date("Y-m-d"strtotime($row_rsCiviEventResults['end_date']))."|end date: ".date("Y-m-d"strtotime($row_rsCiviEventResults['start_date']))); ?>




what's that give?

Sign in to reply to this post

John LangerBeta Tester

It returns this:

start date: 2010-09-24|end date: 2010-09-24

You can see it here

Sign in to reply to this post

Jason ByrnesWebAssist

it's the if statement

php:
<?php if ($row_rsCiviEventResults['end_date']!="" || date("Y-m-d"strtotime($row_rsCiviEventResults['end_date'])) !=  date("Y-m-d"strtotime($row_rsCiviEventResults['start_date'])) ){ ?>




the says:
if the end date is not blank OR the end date does not equal the start date.

since the end date is not blank, the condition is met. use AND instead:

php:
<?php if ($row_rsCiviEventResults['end_date']!="" && date("Y-m-d"strtotime($row_rsCiviEventResults['end_date'])) !=  date("Y-m-d"strtotime($row_rsCiviEventResults['start_date'])) ){ ?>
Sign in to reply to this post

John LangerBeta Tester

I said it was something simple :). I did try that before but before you put me right with the strtotime part.

Yes that worked. Well done. Many thanks as always.

Sign in to reply to this post

Jason ByrnesWebAssist

you're welcome.

Sign in to reply to this post
loading

Build websites with a little help from your friends

Your friends over here at WebAssist! These Dreamweaver extensions will assist you in building unlimited, custom websites.

Build websites from already-built web applications

These out-of-the-box solutions provide you proven, tested applications that can be up and running now.  Build a store, a gallery, or a web-based email solution.

Want your website pre-built and hosted?

Close Windowclose

Rate your experience or provide feedback on this page

Account or customer service questions?
Please user our contact form.

Need technical support?
Please visit support to ask a question

Content

rating

Layout

rating

Ease of use

rating

security code refresh image

We do not respond to comments submitted from this page directly, but we do read and analyze any feedback and will use it to help make your experience better in the future.

Close Windowclose

We were unable to retrieve the attached file

Close Windowclose

Attach and remove files

add attachmentAdd attachment
Close Windowclose

Enter the URL you would like to link to in your post

Close Windowclose

This is how you use right click RTF editing

Enable right click RTF editing option allows you to add html markup into your tutorial such as images, bulleted lists, files and more...

-- click to close --

Uploading file...