date

Description

Returns a formatted date.

 string date( string format [, int timestamp ])

Returns the date, formatted according to the rules of Java’s SimpleDateFormat class.

See the class documentation for the full list of letter codes; here are the most useful:

Letter Date or Time Component Examples
y Year 2012; 12
M Month in year July; Jul; 07
d Day in month 10
E Day in week Tuesday; Tue
a AM/PM marker PM
H Hour in day (0-23) 0
h Hour in am/pm (1-12) 12
m Minute in hour 30
s Second in minute 55

Pattern letters are usually repeated. The number of repetitions determines their presentation, add more or less repetitions to get more or less abbreviated names.

Example of a day of the week:

{date("E")}    --> Mon
{date("EEEE")} --> Monday

By default, the current date, or the send date of the message, is what is used.

{date('MMM dd, yyyy')} ---> Jul 30, 2012
{date('MM/dd/yyyy')}   ---> 07/30/2012
{date('MM/dd/yy')}      ---> 07/30/12

 

If you pass a second timestampparameter as a UNIX timestamp, it will format the date for that timestamp.

See the time( )function for a way to generate such timestamps, for example:

{date('MMM dd, yyyy', time('Jul 15, 2012 +1 week'))} ---> Jul 22, 2012

In addition, you can use the date( ) dynamically in an email with the time( ). For example:

{date("MMM dd, yyyy", time("+1 day"))}  ---> tomorrow's date

 

Examples

Convert UNIX time to date when pulling from a data feed:

{date('MMM dd, yyyy', content[0].date)}


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.



Format Date from a Content Feed

Use Case: You want to pass and display the publish date and time of an article from a Sailthru Content Feed. Since Sailthru automatically adds this parameter as a timestamp to your content, you can format simply with the data() function.

Zephyr:

Your top story for the day:<br/>
<a href="{content[0].url}">{content[0].title}</a> published on {date("MMMM dd, YYYY", content[0].date)} at {date("hh:mm:ss aa", content[0].date)}

Output:

Your top story for the day:

Stephen King’s New Book is a Real Scream! published on March 13, 2017 at 12:42:39 PM

Description:

This script uses the date() function to format the UNIX timestamp on a piece of content from a Sailthru Content Feed (note that Sailthru automatically adds the “date” parameter and value upon spider). It converts it to a human-readable format in two steps: First to format the month, day, and year; and then to format the hour, minute, second, and produce the AM/PM marker.



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.



Add Ordinal Dates to the Current Date

Use Case:

You want to add the ordinal date (i.e. “st” to “January 1) based on the current date.

Zephyr:

In the Setup:

 

{*Ordinal Date*}
{suffixes = [“th”, “st”, “nd”, “rd”, “th”, “th”, “th”, “th”, “th”, “th”,”th”, “th”, “th”, “th”, “th”, “th”, “th”, “th”, “th”, “th”,”th”, “st”, “nd”, “rd”, “th”, “th”, “th”, “th”, “th”, “th”, “th”, “st”]}
{month = date(‘MMMM’)}
{day = date(‘d’)}
{suffix = suffixes[int(day)]}
{ordDate = month + ” ” + day + suffix}

 

In the Code:

<p>Today is {ordDate}!</p>

Output:

Today is February 29th!

Explanation: This script uses the date() function to isolate the current month and and day, with the output for the day being “1”, “2”, “3”, etc. There is also an array called “suffixes” created where each suffix is in its proper numerical position (i.e. “st” is in the first position, “nd” in the second position, and “rd” in the third, so that “1st”, “2nd”, and “3rd” will be outputted. There’s also a local variable called “suffix” uses the numerical value of the current day, created using the int() function. This number is used to find the proper suffix in the “suffixes” array. For instance, if today is March 2, the value of “suffix” is suffixes[2], which equates to “nd”. Finally, an “ordDate” variable is created, which is the concatenation of the current month plus the current day plus its proper suffix.



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