time

Welcome to our newly overhauled Zephyr pages! See our new Overview, Examples, and Index.
Feedback is welcome at bottom of each page.

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 nowparameter.

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.

Examples

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

Show Today’s Date in Reader-Friendly Format

Use Case: Show the current date in an email body/subject line in any desired format. Note that this also uses the time() function.

Zephyr:

Today's date is {date('MMMM dd, yyyy',time("now"))}

Output:

Today’s date is August 01, 2016.

Explanation: This script uses the time() function to return the UNIX timestamp for the current date and time, then uses date() to convert that timestamp to the common format of Month Day, Year.

Dynamic Coupon Offer

Use Case: Check a user’s profile to see when they last purchased, and if it’s over six months ago, show a coupon for a week.

Zephyr:

{if profile.purchases[length(profile.purchases)-1].time > time("-187 days") && profile.purchases[length(profile.purchases)-1].time < time("-180 days")}
You haven't purchased in over six months! Here's a coupon!
{/if}

Explanation:

The script uses the length() function to find the entire number of purchases a user has ever made. Since arrays start with 0, subtract 1 from this value to isolate the last item in this array since arrays start at 0. For example, if a user has purchased ten items, the length() function would return the number 10, which we then subtract 1 from, giving us profile.purchases[9].time. “profile.purchases” references the “purchases” array on the profile object (i.e. the user’s entire purchase history), and profile.purchases[9].time specifically reference the timestamp of their 10th purchase.


It then uses the time() function to create two UNIX timestamps: One for 180 days ago, and one for 187 days ago. It then uses an if statement to make the evaluation if that timestamp is greater than (“>”) 187 days ago and (“&&”) less than (“<“) 180 days ago, i.e. the user’s last purchase was between 180 and 187 days. If you have this banner in a daily mailing, the user will then have the opportunity to see it for a full week.

Set Current Date/Time as User Profile Field Value

Use Case: Used in conjunction with the date() function, apply a date value to a user profile after they receive a “Subscription Cancellation” template in order to re-target down the line (ex. 30 days later, send an offer for 15% off if they resubscribe)

Zephyr:

{api_user({"vars" : {"canceled_date" : date('yyyy-MM-dd', time("now"))}})}

Output:

zephyr example canceled_date

Explanation:

This script uses the api_user() function to set a custom field (called “canceled_date”) on a user upon receipt of a transactional template (in this scenario, a Cancellation Confirmation email). To generate the value, we use the time() function to produce the current timestamp and the date function to format it in the UNIX YYYY-MM-DD format, which is a requirement for storing a date value on the user profile. This can be beneficial for future retargeting, such as creating a daily Recurring Campaign with a Smart List of users whose “canceled_date” variable is exactly 30 days ago.

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.

Show a Countdown Clock Based On a User Variable

Use Case: You want to find out how to see how many days ago a person’s birthday was. This requires having a user’s birthday set as a var on the user profile (ex. birthay_date) in the format of YYYY-MM-DD or YYYYMMDD. Eventually, you’d like to use this to target specialized promotions (i.e. if they haven’t purchased an item since their birthday).

Used in conjunction with date(), time(), int(), strpos(), and substr(). Let’s assume the current date is January 1st.

Zephyr:

On the User Profile:

zephyr example birthday_date

In the Setup:

{birthdayNoYear = substr(birthday_date,strpos(birthday_date,'-')+1)}
{currentYear = int(date('YYYY'))}
{currentBirthday = currentYear + '-' + birthdayNoYear}
{yearStart = time(currentYear+"-01-01")}
{birthdayTime = (time(currentBirthday) - time("now"))/86400}
{if type((currentYear+1)/4) == "float"}
{leapYear = false}
{else}
{leapYear = true}
{/if}

{if date("MM-dd") == birthdayNoYear}
{birthdayCountdown = 0}

{else if birthdayTime < 1 && birthdayTime > 0}
{birthdayCountdown = 1}

{else if birthdayTime < 0 && leapYear == false}
{birthdayCountdown = int(birthdayTime) + 365}
{birthdayPass= true}

{else if birthdayTime < 0 && leapYear == true}
{birthdayCountdown = int(birthdayTime) + 366}
{birthdayPass = true}

{else}
{birthdayCountdown = int(birthdayTime) +1}
{/if}

In the Code:

{if birthdayCountdown == 0}
Happy birthday!
{else if birthdayCountdown == 1}
Your birthday is tomorrow!
{else if (birthdayCountdown == 364 && leapYear == false) || (birthdayCountdown == 365 && leapYear == true)}
Your birthday was yesterday!
{else if birthdayPass == true}
Your birthday passed this year, but the next one is in {birthdayCountdown} days!
{else}
Your birthday is in {birthdayCountdown} days!
{/if}

Output:

Your birthday passed this year, but the next one is in 212 days!

Show/Hide Code Explanation

This script uses substr() to take a sub-string of a custom field, in this instance, the user’s birthday (stored as “birthday_date), to retrieve the day and month of the user’s birth, leaving out the year. Since the birthday value is stored as YYYY-MM-DD, the function takes the value of that custom field, and using the strpos() function (aka string position) finds the numerical position fo the first “-” and adds one, meaning to retrieve everything after the year and dash. For example, if it were stored as “1987-08-01″, subtr() and strpos() in conjunction would return “08-01″ as the sub-string value. This value is stored as “birthdayNoYear”.

The date() function is then used to isolate the current year, and int() is used to turn in from a string to an integer, and finally the value is stored in the “currentYear” variable. A “currentBirthday” variable is created, which is the concatenation of the currentYear value plus a hypen plus the user’s birth month and day. For instance, using the same user example and if the current year is 2017, currentBirthday would equate to 2017-08-01. Next, the time() function is used to get the UNIX timestamp for the current start of the year, i.e. the value of “currentYear” plus “01-01″. The UNIX timestamp of the user’s current birthday is also needed, which is retrieved using the time() function on the “currentBirthday” value minus the UNIX timestamp of the current time, and finally dividing that value by 86400, i.e. the total number of seconds in a day. The value is stored as “birthdayTime”, and the value is the number of days left until the user’s birthday.

In order to determine if the upcoming year is a leap year, “1” is added to the current year and then divided by 4. The data type of the value is checked; if it equals “float”, the following year is not a leap year, and a variable called “leapYear” is set to “false”. Otherwise if the data type is not a float, leapYear is set to “true”. Note that leap years are wholly divisible by 4, meaning there’s no decimal place, and a “float” data type is one that contains a decimal place, so any value with a decimal was not wholly divisible, and thus not a leap year.

For the following conditional statement, int() will be used regularly to round to the nearest whole number for reader-friendliness later on. The “if” statement is used to determine if the user’s birthday is today, if it has passed for the year, or if it’s still upcoming for the year. The date() function checks to see if the current month and day is equal to the user’s “birthdayNoYear” value (their birth month and day). If so, it sets a “birthdayCountdown” variable to 0. Next it checks if checks if the value for “birthdayTime” is greater than 1 and less than 0 (which indicates that their birthday is the next day). If it is, it sets the “birthdayCountdown” variable to 1. For instance, for the example user, at 12PM on July 31st, their “birthdayTime” value would be “.5″. Setting it to 1 lets us know later on that the user’s birthday is tomorrow.

The next check is to see if their birthdayTime less than 0 (indicating that their birthday has passed this year). If it is and leapYear is false, then the “birthdayCountdown” variable is set to the value of the integer of their current “birthdayTime” (using the int() function) plus 365. A “birthdayPass” variable is also set to “true”. Next, we check the same criteria, except if “leapYear” evaluates to true, the “birthdayCountdown” variable is the value of the integer of the current “birthdayTime” (again using the int() function) plus 366. “birthdayPass” is likewise set to “true”. If none of these statements were true, then “birthdayCountdown” is set to the integer of the “birthdayTime” value plus 1. Since taking an integer rounds the value down (for example, {int(3.2)} becomes 3), adding one gives us the true number of days left until the user’s birthday.

Finally, in the code, another “if” statement evaluates the value of the “birthdayCountdown” variable. If it’s 0, indicating that there are zero days left until the user’s birthday, display a “Happy birthday!” message. If it’s 1, display a message telling the user their birthday is tomorrow. Next, if it’s not a leap year and there are 364 days left until the user’s birthday or if it is a leap year and there are 365 days left, these both indicate that the user’s birthday was yesterday, and a message saying their birthday was yesterday appears. Next, if the “birthdayPass” value evaluates to true, a message indicates as such and populates the number of days left until their next birthday. Finally, the last scenario is that their birthday hasn’t occurred yet this year. If that’s the case, a message displaying the number of days left until their birthday appears.

Show a Prompt for a User to Login Based on a “last_login_date” Custom Field

Use Case: You’re storing as a user variable “last_login_date” and want to find the number of days since a user last logged in. Using abs(), time(), and int(), you can output a reader-friendly to encourage people to log back in.

Zephyr:

{daysSinceLastLogin = (time(last_login_date) - time("now"))/86400}
{friendlyDays = int(abs(daysSinceLastLogin))}
You haven't logged in in the past {friendlyDays} days!

Output:

On the user profile:

zephyr example last_login_date

“You haven’t logged in in the past 5 days!”

Explanation: This script uses the time() function to create a UNIX timestamp of each user’s last_login_date, which then subtracts the value of the current UNIX timestamp, and finally divided by 86400, the number of seconds in a day. This provides the number of days since the user’s last login, which is display as a negative float number, for example, -82.4795. Using the abs() function, the absolute value of that number is taken, converting -82.4795 to 82.4795. Within the same script, the int() function is used to round that value to the nearest whole number, converting 82.4795 to simply 82. This value is assigned to the local variable “friendlyDate”. Finally, the “friendlyDate” variable is called within the template, and the number of days since last login will display for each user.

Override User Interest Level with Custom Field Data (Tag Only)

Use Case: You have an array of interests that you capture on your users upon signup, and for the first month a user is subscribed, you’d like to use those interests to recommend content until while Sailthru ingests user interest data.

Zephyr:

On the profile:

zephyr example my_interests array

In the Setup:

{if profile.signup_time < time("30 days")}
{horizon_set_interest(profile.vars.my_interests,5)}
{/if}

This will, for the purpose of that send (and for the first 30 days of the user being on the list), inflate those three values to a level of 5.

Explanation: This script uses the horizon_set_interest() function to adjust user interest level data based on a custom field collected stored as an array of values. As this can be beneficial to show new users content during their first few weeks post signup, this script is wrapped in a conditional “if” statement. If the user’s signup time is less than 30 days ago (i.e. they’ve signed up in the last 30 days), run the horizon_set_interest() script to set the interest level of each to 5x that average level. Otherwise, Sailthru will use the interest generated purely from on-site browsing.

Override User Interest Level with Custom Field Data (Tag and Score)

Use Case: You have an array of interests and an applicable score that you capture on your users upon signup, and for the first month a user is subscribed, you’d like to use those interests to recommend content until while Sailthru ingests user interest data.

Zephyr:

On the profile:
zephyr example my_interests_score tag array

In the Setup:

 

{if profile.signup_time < time(“30 days”)}
{foreach my_interests_score as c}
{horizon_set_interest(c.tag,c.score,true)}
{/foreach}
{/if}

This will, for the purposes of that send (and for the first 30 days of the user being on the list), inflate those three values to the “score” level on in the custom field. However, by having “true” in the third field, the level won’t change unless the actual score is higher (i.e. if my Sailthru-captured user interest is now 10, it won’t be reduced to a level of 1).

Explanation:

This script uses the horizon_set_interest() function to adjust user interest level data based on a custom field collected, in this instance, tags with an applicable “score.” The foreach statement loops through this custom field and sets whatever is stored as the “score” value and applies it to the applicable “tag” value for each user. If the current Sailthru-tracked user interest level for that tag is higher than the value in the custom field, the “true” parameter allows Sailthru to use that higher value.

As this can be beneficial to show new users content during their first few weeks post signup, this script is wrapped in a conditional “if” statement. If the user’s signup time is less than 30 days ago (i.e. they’ve signed up in the last 30 days), run the horizon_set_interest() script. Otherwise, Sailthru will use the interest generated purely from on-site browsing.

Cancel and Resend Template Based on a Date (Avoiding Weekends)

Use Case: You have a certain transactional (such as a Welcome email) that you don’t want going out on the weekends. Instead, you’d like it to go out the following Monday.

Zephyr:

In the Setup:

{cancel(date("EEEE", time('now')) == "Saturday" || date("EEEE", time('now')) == "Sunday", "It's the weekend")}

In the Trigger:

Event: On Cancel
Time: 0 minutes
Action: Custom Zephyr:

{if date("EEEE",time('now'))=="Sunday"}
{api_send({'template':Welcome:', 'schedule_time':date("EEEE, MMMM d, yyyy hh:mma",time("+24 hours"))})}
{else if date("EEEE",time('now'))=="Saturday"}
{api_send({'template':Welcome:', 'schedule_time':date("EEEE, MMMM d, yyyy hh:mma",time("+48 hours"))})}
{/if}

Explanation: 

This script uses the time() function to return the timestamp of the current day and time, which the “date” function converts to a human readable value. Then using the cancel() function, an evaluation is made to check if the value equals “Saturday” or “Sunday”. Note: The double pipe  (“||”) acts as on “OR” statement.

Assuming that this is the only cancel() statement within the template, a script is triggered in the “Triggers” tab on event send. Using an “if” statement, and similarly using the date() and time() functions, a check is done to see which day it is. If it’s Sunday, the api_send() function will queue the template to resend itself in 24 hours (i.e. on Monday) at the current time. So if the template canceled at 12:30PM on Sunday, it will resend at 12:30PM on Monday. Similarly, if the template canceled on Saturday, api_send() will queue it to resend to the user in 48 hours, i.e. the following Monday.

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.

Top