time

Returns a timestamp.

 integer time( [ string input [, integer now ]] )

When called with no parameters, returns the current UNIX timestamp (the number of seconds since January 1 1970 00:00:00 GMT). When a string input parameter is passed, attempts to convert the string to a timestamp, in a manner similar to (but not identical to) PHP’s strtotime function. If a timestamp (in milliseconds) is passed to the now parameter, it uses that value as a basis for calculating relative times. The time() function’s input function is quite generous and understands combinations of formats such as the following:

 "Jan 31, 2012" "1/31/12" "January 31" "3:32 PM" "15:32:00" "3 PM"

You can apply relative times using + or - like so:

 "+60 minutes" "-3 hours" "+2 weeks" "+6 months" "-1 year"

These relative times are applied relative to the now parameter. You can get the next available day of the week by passing a weekday name, for instance:

 "+2 weeks Wednesday 3 PM"

Will return the Wednesday following two weeks from the current date, at 3 PM.

Notes on the time() function

  • When the user is viewing the web/browser-based version of an email, the page-load time is used. To ensure you are using the time that the email was sent, use the date function.
  • Dates and times are parsed separately, and must be separated by a space.
  • The time always defaults to the client’s timezone. A timezone cannot be designated.

Examples

{if profile.click_time > time("-4 weeks")} 
   This user has clicked within the past four weeks 
{/if}

Create a Countdown Clock for a Certain Date

Use Case: If you want to create a countdown clock until a certain date (for example, Christmas), you can use int() in conjunction with time() in order to subtract and get the whole-number remainder.

Zephyr:

{currentYear = date('yyyy')}
{christmasTime = (time("December 25, +currentYear+ 00:00:00") - time("now"))/86400}
{if christmasTime < 1 && christmasTime > 0}
   {christmasCountdown = 1}
{else}
   {christmasCountdown = int(christmasTime)}
{/if}
{if christmasCountdown > 0}
There are {christmasCountdown} days left until Christmas!
{/if}

Output:
christmasTime = 25.508055555555554 int(christmasTime) = 25

Explanation:
This script uses the date() function to isolate the current year, and within a local variable called “christmasTime”, it appends that value on to “December 25,” and from there uses time() to generate a UNIX timestamp, which is the timestamp of Christmas that year. That value is then subtracted by the current timestamp and then divided by 86400, which is the number of seconds in a day. This gives us the number of days left until Christmas. Since this renders as a “float” (i.e., has a decimal place), an “if” statement is used to to find any numbers between 0 and 1 (for example, “.5”, which would be roughly 12 hours left until Christmas) and set the value of a local variable called “christmasCountdown” to “1”, as this means Christmas is the next day. Otherwise, using the int() function, the float value “christmasTime” variable is rounded down to the nearest whole number and assigned to the “christmasCountdown” variable. Finally, the message is wrapped in a conditional checking if the value for “christmasCountdown” is greater than 0. If it is, display the message.

Include Dynamic Date for ‘Email Pause’ Opt-down Option in Optout Page

Use Case: You want to let users “pause” their subscription by setting a custom field on their profile called “optback_date.” You’ll then add these users to a suppression list to avoid mailing them until their selected date has passed:

Zephyr:

In a Hosted Page:
<form method="post">

<p>When do you want to hear from us again?</p>

<label><input type="radio" name="vars[optback_date]" value="{date('yyyy-MM-dd', time("+30 days"))}" data-type="date"/> Mail me again in 30 Days ({date('MMMM dd, yyyy', time("+30 days"))})</label>

<br/><br/>

<label><input type="radio" name="vars[optback_date]" value="{date('yyyy-MM-dd', time("+60 days"))}" data-type="date"/> Mail me again in 60 Days ({date('MMMM dd, yyyy', time("+60 days"))})</label>

<br/><br/>

<label><input type="radio" name="vars[optback_date]" value="{date('yyyy-MM-dd', time("+90 days"))}" data-type="date"/> Mail me again in 90 Days ({date('MMMM dd, yyyy', time("+90 days"))})</label>

<br/><br/>

<label><input type="radio" name="vars[optback_date]" value="{date('yyyy-MM-dd', time("now"))}" data-type="date"/> I miss you! Resubscribe Me.</label>

<br/><br/>

<input type="submit" value="Submit"/>

</form>

Output:

You can then create a suppression list to suppress any user whose “optback_date” value is after today:

Explanation:

This script uses the date() and time() functions to set a dynamic value for a custom field on a user profile called “optback_date”. The time() function generates the UNIX timestamp for 30, 60, and 90 days from now, and the date() function formats them as YYYY-MM-DD (required format for setting a “date” variable in Sailthru). This value is then set on a user’s profile as the optback_date, and any user who has a value for that comes after today’s date, they’ll be added to a suppression list. The code also includes a way for users to resubscribe by resetting that value to today’s date, using the time() function to pull the current UNIX timestamp, and again formatting it as YYYY-MM-DD with the date() function.

Contact us

Top