PHP: Date conversion and formatting functions

I find I often need to convert either a UNIX timestamp (e.g. 1545146453) or an ISO/SQL date (e.g. 2018-12-18) into something else; here is the collection of functions I use.

They are divided into two groups: the functions in one are for timestamps and are called ts2something(); the functions in the other are for ISO dates and are called iso2something().

The something bit of the name can be replaced with any of the following:

A couple of examples:

Here are the functions for converting from UNIX timestamps:

<?php

// ts2iso: ISO format (2018-12-18)
function ts2iso ($ts)
{ 
  return date("Y-m-d", $ts);						
} 

// ts2dmy: numeric day-month-year format (18/12/2018)
function ts2dmy ($ts)
{ 
  return date("d/m/Y", $ts);						
}

// ts2mdy: numeric month-day-year format (12/18/2018)
function ts2mdy ($ts)	
{ 
  return date("m/d/Y", $ts);						
} 

// ts2short: short format (18 Dec 2018)
function ts2short ($ts)
{ 
  return date("j M Y", $ts);						
}

// ts2long: long format (18th December 2018)
function ts2long ($ts) 
{
  return date("jS F Y", $ts);						
}

// ts2full: full format (Tuesday, 18th December 2018)
function ts2full ($ts) 
{
  return date("l, jS F Y", $ts);						
}

// ts2http: HTTP "Last-Modified" format (Tue, 18 Dec 2018 15:20:53 GMT)
function ts2http ($ts) 
{ 
  return gmdate("D, d M Y H:i:s", $ts). " GMT";				
}

?>
		

And here are their equivalents for ISO dates:

<?php

// iso2ts: UNIX timestamp format (1545134400)
function iso2ts ($iso, $hour=12, $min=0, $sec=0) 
{
  $d = substr($iso, 8, 2);
  $m = substr($iso, 5, 2);
  $y = substr($iso, 0, 4);
  return mktime($hour, $min, $sec, $m, $d, $y);
}

// iso2dmy: numeric day-month-year format (18/12/2018)
function iso2dmy ($iso)
{ 
  return ts2dmy(iso2ts($iso));
}

// iso2mdy: numeric month-day-year format (12/18/2018)
function iso2mdy ($iso)
{ 
  return ts2mdy(iso2ts($iso));					
}

// iso2short: short format (18 Dec 2018)
function iso2short ($iso)	
{ 
  return ts2short(iso2ts($iso));					
}

// iso2long: long format (18th December 2018)
function iso2long ($iso) 
{ 
  return ts2long(iso2ts($iso));						
}

// iso2full: full format (Tuesday, 18th December 2018)
function iso2full ($iso) 
{ 
  return ts2full(iso2ts($iso));						
}

// iso2http: HTTP "Last-Modified" format (Tue, 18 Dec 2018 15:20:53 GMT)
function iso2http ($iso, $hour=12, $min=0, $sec=0)
{ 
  return gmdate("D, d M Y H:i:s", iso2ts($iso, $hour, $min, $sec)). " GMT";	
}

?>
		

Note: iso2ts() and iso2http() have three optional parameters which you can use to specify the hour, minute and second for the output. If you ignore them, you'll get the default of 12 noon local time.

Please do get in touch if you have any questions or suggestions for improvement.