View previous topic :: View next topic |
Author |
Message |
clj83 Smarty Regular
Joined: 16 Oct 2009 Posts: 44
|
Posted: Thu Nov 18, 2010 10:31 pm Post subject: Subtract Two Dates And Show New Date? |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Nov 18, 2010 10:44 pm Post subject: |
|
|
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 |
|
clj83 Smarty Regular
Joined: 16 Oct 2009 Posts: 44
|
Posted: Thu Nov 18, 2010 10:59 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Thu Nov 18, 2010 11:06 pm Post subject: |
|
|
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 |
|
clj83 Smarty Regular
Joined: 16 Oct 2009 Posts: 44
|
Posted: Thu Nov 18, 2010 11:24 pm Post subject: |
|
|
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 |
|
mohrt Administrator
Joined: 16 Apr 2003 Posts: 7368 Location: Lincoln Nebraska, USA
|
Posted: Fri Nov 19, 2010 12:23 am Post subject: |
|
|
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 |
|
clj83 Smarty Regular
Joined: 16 Oct 2009 Posts: 44
|
Posted: Fri Nov 19, 2010 11:25 am Post subject: Thanks |
|
|
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 |
|
|