Problems, need help? Have a tip or advice? Post it here.
15 posts Page 1 of 2
Hi.
I need to get the current date and then test if another date stored in a Couch field is earlier. I think the "lt" less than operator should work once I can find a way to return the current date. Something like <cms:if start_date lt current_date > I tried inserting php code and echoing the date() function however that throws an error. How can I accomplish this?

Thanks.
Lots of style and minimal fuss, if only everyday could be that easy.
Hi,

The cms:date tag if used without any explicit 'date' to format, returns the current date.
Following is how we can use it to get the current date -
Code: Select all
<cms:set cur_date="<cms:date format='Y-m-d H:i:s' />" />

Following is an example of how we can compare two dates -
Code: Select all
<cms:set start_date='2015-04-15' />
..
..
<cms:set cur_date="<cms:date format='Y-m-d H:i:s' />" />

<cms:if start_date lt cur_date >
    date lt
<cms:else />
    date gt
</cms:if>

Please note that dates are stored internally in Couch using 'Y-m-d H:i:s' format.
If you wish to compare dates, the two should be in the said format.

Hope this helps.
regarding this.. is there also a way to get the difference between the days (day count) ?

Today: 17-4-2014
Event day: 20-4-2014

Show: 3 days left.
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
regarding this.. is there also a way to get the difference between the days (day count) ?

Today: 17-4-2014
Event day: 20-4-2014

Show: 3 days left.


Is there a solution to this problem? would be extremely useful.
Hi Wizardradio,

I can't remember how and if I did solve this at the time... but I could find a solution I used in the past from stackoverflow http://stackoverflow.com/questions/676824/how-to-calculate-the-difference-between-two-dates-using-php. Maybe this helps if you use it in your kfunctions.php file.
I load frameworks and write bugs on top of them, after that I rearrange the code so that it looks like a cool product.
wizardradio wrote:
regarding this.. is there also a way to get the difference between the days (day count) ?

Today: 17-4-2014
Event day: 20-4-2014

Show: 3 days left.


Is there a solution to this problem? would be extremely useful.


Sorry to resurrect an old thread but it came up during my searches trying to find couch code for this problem. I could write PHP directly in the template or using kfunctions, however did not know if this was possible using native couch code. Trying to keep site as couchish as possible for future designers who will work on the site.

I already have this code, comparing today's date to the date entered in admin using the date picker.

price = regular price
eb_price = early bird pricing (discounted)
end_date = from admin using date picker to set date the eb_price is no longer valid.

Code: Select all
<cms:set current_date = "<cms:date format='Y-m-d H:i:s' />" />
<cms:if (current_date lt end_date) >
    $<span class="price"><span class="strike"><cms:show price /></span> $<cms:show eb_price /></span>
<cms:else />
    $<span class="price"><cms:show price /></span>
</cms:if>


Works great for showing the price and strikethrough on regular price.

Now I would like to add a line below the pricing letting users know the days left of this offer.

Code: Select all
<span>There's <cms:show days_left /><cms:if days_left == 1>day<cms:else />days</cms:if> left of this special pricing!</span>
The time since the Unix Epoch is a useful format for doing arithmetic with dates.

Code: Select all
    <!-- Strip off hours, minutes, seconds
    Then convert to time since Unix Epoch -->
    <cms:set today = "<cms:date format='Y-m-d' />" />
    <cms:set today = "<cms:date today format='U' />" />

   <!-- Assumes end_date is already formatted without hours, minutes and seconds -->               
    <cms:set end = "<cms:date end_date format='U' />" />
    <cms:set diff = "<cms:sub end today />" />
               
    <!-- Convert seconds to days -->
    <cms:set diff = "<cms:div diff '86400' />" />
    <cms:set days_left = "<cms:add diff '1' />" />

    <cms:show days_left />
Hi Tim,

Tried the code, getting 16.9583333333 for the answer.

end_date is whatever is stored by the datepicker KK had posted, if I do a <cms:show end_date /> this is what I get:

2016-03-15

Thanks
Weird. It seems to be going wonky above 15 days. I don't know why that's happening, but the following corrects for it.
Code: Select all
 <!-- Strip off hours, minutes, seconds
    Then convert to time since Unix Epoch -->
    <cms:set today = "<cms:date format='Y-m-d' />" />
    <cms:set today = "<cms:date today format='U' />" />

   <!-- Assumes end_date is already formatted without hours, minutes and seconds -->               
    <cms:set end = "<cms:date end_date format='U' />" />
    <cms:set diff = "<cms:sub end today />" />
               
    <!-- Convert seconds to days -->
    <cms:set diff = "<cms:div diff '86400' />" />
    <cms:set days_left = "<cms:number_format "<cms:add diff '1.1' />" decimal_precision='0' />" />

    <cms:show days_left />
Hi Tim, looks like that did it thanks!
15 posts Page 1 of 2