Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

Subtract Two Dates And Show New Date?

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
clj83
Smarty Regular


Joined: 16 Oct 2009
Posts: 44

PostPosted: Thu Nov 18, 2010 10:31 pm    Post subject: Subtract Two Dates And Show New Date? Reply with quote

Hi,

I have the date_diff function working and that gives me the difference between two dates in terms of days, months or years. However, I was wondering if it was possible to subtract one date from another and show the answer as a complete date?

I.E. 2011/12/01 - 2010/06/01 = ??/??/??

Is this possible with smarty?

Thanks

Chris
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Nov 18, 2010 10:44 pm    Post subject: Reply with quote

That doesn't make sense. What it the answer to your example?


I.E. 2011/12/01 - 2010/06/01 = ??/??/??


You can get the number of seconds between the dates, but there isn't a "date" that equals to.
Back to top
View user's profile Send private message Visit poster's website
clj83
Smarty Regular


Joined: 16 Oct 2009
Posts: 44

PostPosted: Thu Nov 18, 2010 10:59 pm    Post subject: Reply with quote

Yes, your right. On reflection what I need is to minus a fixed number of days from a date.

So for example,

18/11/2010 - 7 Days = 11/11/2010

Is that possible?

Thanks for your time

Chris
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Thu Nov 18, 2010 11:06 pm    Post subject: Reply with quote

date_format will take anything strtotime() will process, so this works:

{"-7 days"|date_format:"%Y-%m-d%"}

If you want a date exactly 7 days ago.
Back to top
View user's profile Send private message Visit poster's website
clj83
Smarty Regular


Joined: 16 Oct 2009
Posts: 44

PostPosted: Thu Nov 18, 2010 11:24 pm    Post subject: Reply with quote

Hi,

Thanks for that, I cant quite get it working though. Is this correct,

{$date|"-7 days"|date_format:"%Y-%m-d%"}

Im guessing not as it doesnt work, or any of the other variations I have tried.

Thanks

Chris
Back to top
View user's profile Send private message
mohrt
Administrator


Joined: 16 Apr 2003
Posts: 7368
Location: Lincoln Nebraska, USA

PostPosted: Fri Nov 19, 2010 12:23 am    Post subject: Reply with quote

its only working on now(), not an arbitrary date, as in my example.

You could subtract 86400*7 from a datestamp, then pass that to date_format. Or write a plugin that does this in PHP:

strftime('%Y-%m-%d',(strtotime($date)-(86400*7)));

assuming $date is a date parsible by strtotime().

what is this for specifically? maybe a plugin exists for what you want.
Back to top
View user's profile Send private message Visit poster's website
clj83
Smarty Regular


Joined: 16 Oct 2009
Posts: 44

PostPosted: Fri Nov 19, 2010 11:25 am    Post subject: Thanks Reply with quote

Hi,

Thanks for your helps. I got it working now. I just wanted to create a payment date 6 weeks before another variable date. See below.


<!-- payment date check starts here -->
{capture assign="unix"}{$entry->fields.date->value}{/capture}
{math equation="x - y" x=$unix|strtotime y=3628800 assign="payment_date"}
<!-- payment date check ends here -->
<tr>
<td><em>Payment Due:</em></td>
<td><em>{$payment_date|date_format:"%d/%m/%Y"} (6 weeks before the course date)</em></td>
</tr>


Thanks

Chris
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP