Getting Time And Date Values In Phing

10th June 2011

The tstamp task in Phing can be used to generate a timestamp that can be used anywhere within the current project. The default behaviour of tstamp is to create the properties DSTAMP, TSTAMP and TODAY, which contain time and date values. All you need to do is include the tstamp XML tag in your project somewhere.

  1. <tstamp>
  2. </tstamp>

The properties DSTAMP, TSTAMP and TODAY use the PHP strftime() function as a basis for formatting the time. The following table shows the value of each property.

Property Format Example
DSTAMP %Y%m%d 20110610
TSTAMP %H%M 1210
TODAY %B %d %Y June 10 2011

Once you have added the tstamp XML tag you can then access these properties in any way you want. The following target defines the tstamp tag and then prints out the values of the three properties.

  1. <target name="tstamp">
  2.  
  3. <tstamp>
  4.  
  5. <echo>DSTAMP = ${DSTAMP}</echo>
  6. <echo>TSTAMP = ${TSTAMP}</echo>
  7. <echo>TODAY = ${TODAY}</echo>
  8. </tstamp></target>

When run this prints out the following:

  1. build > printtime:
  2.  
  3. [echo] DSTAMP = 20110610
  4. [echo] TSTAMP = 1125
  5. [echo] TODAY = June 10 2011

I you wanted to add a prefix to these properties then you can do by using the prefix attribute of the tstamp property. Any value you add here will be required to access any of the tstamp properties. For example, if the prefix is set to "mytime" then the DSTAMP property needs to be accessed via "mytime.DSTAMP". The following is an example of all three parameters being accessed in this manner.

  1. <target name="prefixtstamp">
  2. <tstamp prefix="mytime">
  3.  
  4. <echo>DSTAMP = ${mytime.DSTAMP}</echo>
  5. <echo>TSTAMP = ${mytime.TSTAMP}</echo>
  6. <echo>TODAY = ${mytime.TODAY}</echo>
  7. </tstamp></target>

When creating a tstamp element it is also possible to create a custom tstamp property with a custom format. To do this you create a tstamp element with a child format element. The property attribute defines the name of the property to set and the pattern property sets the pattern of the time stamp. This pattern of this format is the same as detailed on the strftime() manual page. The following sets the DATE parameter to be %c, which is the preferred date and time stamp based on local.

  1. <target name="format">
  2. <tstamp>
  3. <format pattern="%c" property="DATE">
  4. </format></tstamp>
  5.  
  6. <echo>DATE = ${DATE}</echo>
  7. </target>

This produces the following output.

  1. build > format:
  2.  
  3. [echo] DATE = Fri Jun 10 21:53:06 2011

The property name of the format element can also be changed via the use of the prefix attribute on the tstamp element.

It is also possible to override one of the default tstamp property names using the format element. It is important to note here that overriding will only work if the tstamp element hasn't already been used. The following example shows a failiure of overriding the tstamp property TSTAMP as the tstamp element has already been used. This example would produce the correct output if the first tstamp element was removed.

  1. <target name="overridefail">
  2. <tstamp>
  3.  
  4. <tstamp>
  5. <format pattern="%c" property="TSTAMP">
  6. </format></tstamp>
  7.  
  8. <echo>TSTAMP = ${TSTAMP}</echo>
  9. </tstamp></target>

You can also set the locale of the formatted time so that it is translated into region specific formats. The locale strings are the same as can be passed to the PHP setlocale() function. This function depends heavily on the server and platform configuration, but it will always default back to the system language if the locale string isn't recognised. The following example sets the locale to the Spainish language (within Spain) and formats the date to include some day and month names to fully display the different language.

  1. <target name="formatspanish">
  2. <tstamp>
  3. <format locale="Spanish_Spain" pattern="%A %d %B (%c)" property="SpanishDate">
  4. </format></tstamp>
  5.  
  6. <echo>SpanishDate = ${SpanishDate}</echo>
  7. </target>

The output of this function is as follows:

  1. build > formatspanish:
  2.  
  3. [echo] SpanishDate = viernes 10 junio (vie 10 jun 2011 20:27:23 BST)

I should note that the above settings "spanish_Spain" is from a Windows environment. For an Ubuntu linux environment I had to install the appropriate language pack and then change the above locale string to the following:

Comments

Permalink

Thanks for posting this reference for dates, I just used them in my deployment scripts to name my directories after the current date and time - the nice examples here made it easy :)

LornaJane (Fri, 03/23/2012 - 06:45)

Permalink

You're welcome Lorna! Glad I could help out :)

philipnorton42 (Sat, 03/24/2012 - 19:50)

Add new comment

The content of this field is kept private and will not be shown publicly.