Skip to content

Time Functions#


Time functions in Miva Merchant enable precise control and manipulation of date and time data within your templates. These functions allow developers to calculate, format, and display time-based information dynamically, making them essential for operations like scheduling, logging, and analytics.

This guide covers the core time functions, their usage, and practical examples.


Core Time Functions#

mktime_t#

Generates a time_t value for a specified date and time.

Syntax:

mktime_t(year, month, dayofmonth, hours, minutes, seconds, timezone)

Example:

<mvt:assign name="g.time" value="mktime_t(2025, 1, 1, 0, 0, 0, 'local')" />
<p>Timestamp for 2025-01-01 00:00:00: &mvt:global:time;</p>


timezone#

Returns the number of hours offset from GMT for the local timezone.

Syntax:

timezone()

Example:

<mvt:assign name="g.timezone_offset" value="timezone()" />
<p>Timezone Offset: &mvt:global:timezone_offset; hours</p>


time_t_year#

Returns the year for a given time_t value.

Syntax:

time_t_year(time_t, timezone)

Example:

<mvt:assign name="g.year" value="time_t_year(s.time_t, 'local')" />
<p>Current Year: &mvt:global:year;</p>


time_t_month#

Returns the month (1-12) for a given time_t value.

Syntax:

time_t_month(time_t, timezone)

Example:

<mvt:assign name="g.month" value="time_t_month(s.time_t, 'local')" />
<p>Current Month: &mvt:global:month;</p>


time_t_dayofmonth#

Returns the day of the month (1-31) for a given time_t value.

Syntax:

time_t_dayofmonth(time_t, timezone)

Example:

<mvt:assign name="g.day" value="time_t_dayofmonth(s.time_t, 'local')" />
<p>Today’s Date: &mvt:global:day;</p>


time_t_hour#

Returns the hour (0-23) for a given time_t value.

Syntax:

time_t_hour(time_t, timezone)

Example:

<mvt:assign name="g.hour" value="time_t_hour(s.time_t, 'local')" />
<p>Current Hour: &mvt:global:hour;</p>


time_t_min#

Returns the minute (0-59) for a given time_t value.

Syntax:

time_t_min(time_t, timezone)

Example:

<mvt:assign name="g.minute" value="time_t_min(s.time_t, 'local')" />
<p>Current Minute: &mvt:global:minute;</p>


time_t_sec#

Returns the second (0-59) for a given time_t value.

Syntax:

time_t_sec(time_t, timezone)

Example:

<mvt:assign name="g.second" value="time_t_sec(s.time_t, 'local')" />
<p>Current Second: &mvt:global:second;</p>


time_t_dayofweek#

Returns the day of the week (1-7, where Sunday=1) for a given time_t value.

Syntax:

time_t_dayofweek(time_t, timezone)

Example:

<mvt:assign name="g.dayofweek" value="time_t_dayofweek(s.time_t, 'local')" />
<p>Day of the Week: &mvt:global:dayofweek;</p>


time_t_dayofyear#

Returns the day of the year (1-366) for a given time_t value.

Syntax:

time_t_dayofyear(time_t, timezone)

Example:

<mvt:assign name="g.dayofyear" value="time_t_dayofyear(s.time_t, 'local')" />
<p>Day of the Year: &mvt:global:dayofyear;</p>


Formatting Dates#

Combine time functions to create formatted timestamps.

Example:

<mvt:assign name="g.year" value="time_t_year(s.time_t, 'local')" />
<mvt:assign name="g.month" value="padl(time_t_month(s.time_t, 'local'), 2, 0)" />
<mvt:assign name="g.day" value="padl(time_t_dayofmonth(s.time_t, 'local'), 2, 0)" />
<mvt:assign name="g.hour" value="padl(time_t_hour(s.time_t, 'local'), 2, 0)" />
<mvt:assign name="g.minute" value="padl(time_t_min(s.time_t, 'local'), 2, 0)" />
<mvt:assign name="g.second" value="padl(time_t_sec(s.time_t, 'local'), 2, 0)" />

<mvt:assign name="g.formatted_date" value="g.year $ '-' $ g.month $ '-' $ g.day $ ' ' $ g.hour $ ':' $ g.minute $ ':' $ g.second" />
<p>Formatted Date: &mvt:global:formatted_date;</p>

Output:

Formatted Date: 2025-01-01 12:30:45


Best Practices#

  1. Use Local Timezones When Necessary:

    • Use the keyword local for time_zone to ensure the time is displayed in the user’s local timezone.
  2. Avoid Hardcoding Time Zones:

    • Use the timezone() function to dynamically retrieve timezone offsets.
  3. Combine Functions for Custom Formats:

    • Leverage multiple time functions to generate user-friendly formats.
  4. Plan for Year 2038 Issues:

    • Be aware that time_t values may encounter limitations beyond January 19, 2038, due to 32-bit integer constraints.

Summary#

Miva’s time functions are essential for managing and displaying date and time information dynamically. By mastering these functions, developers can create templates that are responsive to user needs, schedule events, and format data effectively.