Jump to content

Convert timestamp to date in template.


kotaco

Recommended Posts

Posted

I am trying to convert a timestamp that is recorded when a member joins a certain group to a Month/Date/Year format output in a block template. 

The code I have to display the timestamp is as follows:           

{{$joined = $member->r_spartac_join_date;}}
{{if $member->r_spartac_join_date}}
{$joined}
{{else}}
Returns other stuff if there's no timestamp on record
{{endif}}



The above works well to display the timestamp, but how do I go about converting the time stamp to M/D/Y format?

I tried just wrapping the date function in tags, but it doesn't work: {{date("m-d-Y", $joined);}}

 

Posted
5 hours ago, Ryan Ashbrook said:

{datetime="$joined"}

 

Thank you sir. 

Just curious, is there any way to format that? For example it's showing "18 March" can that be changed to March 18, 2016 easily?

Posted

Hmm... not using the datetime output plugin, no. You can optionally do this:

{datetime="$joined" dateonly="true"}

However that will return a format of 03/17/2016.

You could also do something like this:

{expression="(string) \IPS\DateTime::ts( $joined )->strFormat( '%B %d, %Y' )"}

Which will do what you're looking for. The parameters accepted in strFormat (to format the date) are the same as the strftime PHP function. So you can swap around the %B, %d, etc. however you like to format the date.

  • 3 months later...
Posted

i stumbled upon this  im tring to take the joined data and display the time between them  (tenure)

 

{expression="\IPS\DateTime::ts( $member->joined )->strFormat( '%B %d, %Y' )"}

 

tried this but no luck how would it do this?

Posted
On 26.7.2016 at 5:09 AM, veteran365 said:

i stumbled upon this  im tring to take the joined data and display the time between them  (tenure)

{expression="\IPS\DateTime::ts( $member->joined )->strFormat( '%B %d, %Y' )"}

tried this but no luck how would it do this?

What do you get when using that code and what do you expect? 

If you need to know the difference in terms of number of days or similar between two dates, then that would of course require entirely different code.

  • 3 weeks later...
Posted

did it 

$member = \IPS\Member::loggedIn();
$datetime1 =  $member->joined;
$datetime2 =  new DateTime();
$interval = $datetime1->diff($datetime2);
echo $interval->format('%d Days, %m months, %y Years');

posting here incase someone else needs to do this

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...