php.date ( )

Function stats

Average user rating
29
76
9999
Support
FileMaker 8.5 +
Date posted
17 November 2008
Last updated
31 January 2010
Version
Recursive function
No

Author Info
 Genx

18 functions

Average Rating 4.1

author_avatar



 

Function overview

Prototype

php.date  ( format;   dateVal )


Parameters

format  A string containing placeholders as described at http://php.net/manual/en/function.date.php


dateVal  A date value to work with. If left blank, will use the current date


Description

Tags:  PHP Date   PHP   Format Date   Format   Date  

Function Mimics the php date function. Very powerful date formatting function - should give you all info you ever need. http://php.net/manual/en/function.date.php for a list of possible place holders in the format tag.

Examples

Sample input

php.date( "D F jS H:iA"; "" ) & " and ... " & php.date("Y";"") & " is " & If( not php.date("L"; "" ) ; "not " ) & "a leap year"


Sample output

Tue November 11th 12:27PM and ... 2008 is a leap year

 

Function code

//See: http://php.net/manual/en/function.date.php

Let([
    ts = If( IsEmpty(dateVal) ; Get(CurrentTimeStamp) ; GetAsTimestamp(dateVal) ) ;
    d = GetAsDate(ts);
    t = GetAsTime(ts);
    tmpList = Substitute( format ;
        ["d";"|d|"];
        ["D";"|D|"];
        ["j";"|j|"];
        ["l";"|l|"];
        ["N";"|N|"];
        ["S";"|S|"];
        ["w";"|w|"];
        ["z";"|z|"];

        ["W";"|W|"];
        
        ["F";"|F|"];
        ["m";"|m|"];
        ["M";"|M|"];
        ["n";"|n|"];
        ["t";"|t|"];

        ["L";"|L|"];
        ["o";"|o|"];
        ["Y";"|Y|"];
        ["y";"|y|"];

        ["a";"|a|"];
        ["A";"|A|"];
        ["B";"|B|"];
        ["g";"|g|"];
        ["G";"|G|"];
        ["h";"|h|"];
        ["H";"|H|"];
        ["i";"|i|"];
        ["s";"|s|"];
        ["u";"|u|"]
    )
];
    Substitute( tmpList ;     
    /*** DAY ***/
        ["|d|";Right( "0" & Day (d) ; 2 )];
        ["|D|";Left( DayName(d) ; 3)];
        ["|j|";Day(d)];
        ["|l|";DayName(d)];
        ["|N|";If( DayOfWeek(d) = 1 ; 7 ; DayOfWeek(d) - 1 )];
        ["|S|";Case(
            PatternCount( " 1 21 31 " ; " " & Day ( d ) & " ") ; TextStyleAdd("st";Superscript) ;
            PatternCount( " 2 22 " ; " " & Day ( d ) & " ") ; TextStyleAdd("nd";Superscript) ;
            PatternCount( " 3 23 " ; " " & Day ( d ) & " ") ; TextStyleAdd("rd";Superscript) ;
            TextStyleAdd("th";Superscript)
         ) ];
        ["|w|";DayOfWeek(d) - 1];
        ["|z|";DayOfYear (d) - 1];

    /*** WEEK ***/
        ["|W|";WeekOfYear ( d )];

    /*** MONTH ***/
        ["|F|";MonthName(d)];
        ["|m|";Right( "0" & Month(d) ; 2 )];
        ["|M|";Left(MonthName(d);3)];
        ["|n|";Month(d)];
        ["|t|";Day(Date(Month(d);1;Year(d))-1)];

    /*** YEAR ***/
        ["|L|";If( Day(Date( "2" ; "29" ; Year(d) )) = 1 ; 0 ; 1 )];
        ["|o|";Year(d)];
        ["|Y|";Year(d)];
        ["|y|";Right(Year(d);2)];

    /*** TIME ***/
        ["|a|";If( Hour(t) > 11 ; "pm" ; "am" ) ];
        ["|A|";If( Hour(t) > 11 ; "PM" ; "AM" ) ];
        ["|g|";If( Hour(t) > 12 ; Hour(t) - 12 ; Hour(t)) ];
        ["|G|";Hour(t)];
        ["|h|";Right( "0" & If( Hour(t) > 12 ; Hour(t) - 12 ; Hour(t)) ; 2 ) ];
        ["|H|";Right( "0" & Hour(t) ; 2 ) ];
        ["|i|"; Right( "0" & Minute(t) ; 2) ];
        ["|s|"; Right( "0" & Seconds(t) ; 2 )]
    )
)

// ===================================
/*

    This function is published on FileMaker Custom Functions
    to check for updates and provide feedback and bug reports
    please visit http://www.fmfunctions.com/fid/76

    Prototype: php.date( format; dateVal )
    Function Author: Genx (http://www.fmfunctions.com/mid/29)
    Last updated: 31 January 2010
    Version: 4.2

*/
// ===================================

 

Comments


06 September 2010



Brilliant CF, Genx. Nicely formated too, so it makes it easier to understand its mechanics. Thanks for sharing.
  General comment
dbmike
27 March 2012



This function does not work correctly for times in the 12am hour. The g and h report the hour as "0" and "00", respectively, instead of "12".

I modified to replaced a value of 0 with a value of 12 - the new lines as follows:

        ["|g|";If( Hour(t) > 12 ; Hour(t) - 12 ; If(Hour(t)=0;"12";Hour(t))) ];

        ["|h|";Right( "0" & If( Hour(t) > 12 ; Hour(t) - 12 ; If(Hour(t)=0;"12";Hour(t))) ; 2 ) ];
 Bug report

 

 

 

 

 

Top Tags

Text Parsing  (33)
List  (32)
Date  (28)
XML  (27)
Format  (23)
Sql  (22)
Dev  (20)
Debug  (17)
Interface  (15)
Layout  (15)
Text  (14)
Variables  (13)
Filter  (12)
Layout Objects  (11)
Design  (10)
Array  (8)