summaryrefslogtreecommitdiff
path: root/smartyplugins/modifier.bit_date_format.php
blob: 4a115c39ccb8363d779531cc3d126babb2faa808 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
namespace Bitweaver\Plugins;

use Bitweaver\KernelTools;

/**
 * Smarty plugin
 * @package Smarty
 * @subpackage plugins
 */

/**
 * required setup
 */
global $gBitSmarty;
// $gBitSmarty->loadPlugin('smarty_shared_make_timestamp');

/**
 * Smarty plugin
 * -------------------------------------------------------------
 * Type:     modifier
 * Name:     bit_date_format
 * Purpose:  format datestamps via strftime, (timezone adjusted to administrator specified timezone)
 * Input:    string: input date string
 *           format: strftime format for output
 * -------------------------------------------------------------
 */
function smarty_modifier_bit_date_format( $pString, $format = "%b %e, %Y", $pTraFormat = "%b %e, %Y" ) {
	global $gBitSystem, $gBitUser, $gBitLanguage;

	if( empty( $pString )) {
		return '';
	}

	// we translate the entire date format string for total control
	if( $gBitSystem->getConfig( "bitlanguage", "en" ) != $gBitLanguage->mLanguage ) {
		$format = KernelTools::tra( $pTraFormat );
	}

	if( $gBitUser->getPreference( 'site_display_utc' ) == 'Fixed' && class_exists( 'DateTime' ) ) {
		date_default_timezone_set( $gBitUser->getPreference( 'site_display_timezone', 'UTC' ) );
		try {
			$dateTimeUser = is_numeric( $pString )
				? new \DateTime( '@'.(int)$pString )
				: new \DateTime( $pString );
		} catch ( \DateMalformedStringException | \Exception $e ) {
			return '';
		}
		$disptime = strtotime($dateTimeUser->format(DATE_W3C));
		return $gBitSystem->mServerTimestamp->strftime( $format, $disptime );
	}
		$format = $gBitSystem->get_display_offset()
			? preg_replace( "/ ?%Z/",'', $format )
			: $format = preg_replace( "/%Z/", "UTC", $format );
		if( !is_numeric( $pString ) && strtotime( $pString ) === false ) {
			return '';
		}
		$disptime = $gBitSystem->mServerTimestamp->getDisplayDateFromUTC( $pString );

	return $gBitSystem->mServerTimestamp->strftime( $format, $disptime, TRUE );
}