Escape DateTime

@param $dt \DateTime
@param bool $EMPTY_NULL
@return string
552148
public function escapeDateTime($dt, $EMPTY_NULL = true) {
		/*if(!is_string($s)){
			$s='';
			}*/
		if (($EMPTY_NULL) && (empty($dt))) {
			return 'NULL';
		}
		$str = '"' . $dt->format('Y-m-d H:i:s') . '"';
		return $str;
	}
442891
public function formatDateTimeUtc(\DateTime $date = null)
    {
        if (null === $date) {
            return null;
        }

        $date->setTimezone(new \DateTimeZone('UTC'));

        return $date->format("Y-m-d\TH:i:s\Z");
    }
319426
def sql_datetime_literal(dt: DateTimeLikeType,
                         subsecond: bool = False) -> str:
    
    # ANSI SQL: http://www.contrib.andrew.cmu.edu/~shadow/sql/sql1992.txt
    # <timestamp string>
    # ... the subsecond part is non-ANSI
    fmt = "'%Y-%m-%d %H:%M:%S{}'".format(".%f" if subsecond else "")
    return dt.strftime(fmt)
855594
public static String formatHttpDate(Date date) {
		if (null == date) {
			return null;
		}
		return DatePattern.HTTP_DATETIME_FORMAT.format(date);
	}
1119910
public function buildDateTimeRawValue(\DateTime $dateTime = null) {
		if (null === $dateTime) return null;
		return DateUtils::formatDateTime($dateTime, MssqlDateTimeColumn::generateFormatBuildRawValue(
				MssqlDateTimeColumn::FORMAT_BUILD_TYPE_RAW_VALUE, true, true, self::COLUMN_TYPE_PRECISION, false));
	}
450516
function formatDateTimeType( $fmt, $datetime = false )
    {
        if ( $datetime === false )
        {
            $datetime = time();
        }
        else if ( empty( $datetime ) )
        {
            $datetime = 0;
        }

        $text = date( eZLocale::transformToPHPFormat( $fmt, $this->DateTimePHPArray ), $datetime );
        // Replace some special 'date' formats that needs to be handled
        // internally by the i18n system and not by PHP
        return str_replace( array( '%D', '%l', '%M', '%F',
                                   '%a', '%A' ),
                            array( $this->shortDayName( date( 'w', $datetime ) ),
                                   $this->longDayName( date( 'w', $datetime ) ),
                                   $this->shortMonthName( date( 'n', $datetime ) ),
                                   $this->longMonthName( date( 'n', $datetime ) ),
                                   $this->meridiemName( $datetime, false ),
                                   $this->meridiemName( $datetime, true ) ),
                            $text );
    }
202544
public function min(DateTime $dt = null)
    {
        $dt = $dt ?: static::now($this->timezone());

        return $this->lt($dt) ? $this : $dt;
    }
1213614
public static function stringToDateTime($string, DateTimeZone $timezone = null, $null = null)
    {
        return ValueHelper::isDateTime($string) ? new \DateTime($string, $timezone) : $null;
    }
511962
private String formatDateTimeNull(Date value)
   {
      return (value == null ? m_formats.getNullText() : m_formats.getDateTimeFormat().format(value));
   }
526216
protected function formatDatetimeString($datetime, $format = null)
    {
        $format = $format ?: $this->format;

        if (!$datetime) {
            return null;
        }

        if ($datetime instanceof Carbon) {
            return $datetime->format($format);
        }

        return date($format, strtotime($datetime));
    }
103263
public static function formatDate(DateTime $date, $locale = null, bool $short = false)
    {
        $locale = $locale ?? 'fr';
        $localDates = [
            'fr'    => $short ? 'd/m/y' : 'd/m/Y',
            'en'    => $short ? 'd/m/y' : 'd/m/Y',
            'en_US' => $short ? 'm/d/y' : 'm/d/Y'
        ];
        return (string) $date->format($localDates[$locale]);
    }
1057886
def datetime_fmt(dt)
      if dt.blank? then
        ''
      else
        I18n.l(dt, format: I18n.t('edgarj.time.format'))
      end
    end
187780
public static String datetimeToCustomString(SimpleDateFormat sdf, Date datetime) {
        return Checker.isNull(datetime) ? "" : sdf.format(datetime);
    }
493777
public function formatDate(\DateTime $datetime, $format, $useOffset = null)
    {
        if (is_null($useOffset)) {
            $useOffset = $this->getParameter('nyroDev_utility.dateFormatUseOffsetDefault');
        }

        $offset = 0;
        if ($useOffset) {
            $tz = new \DateTimeZone(date_default_timezone_get());
            $offset = -1 * $tz->getOffset($datetime) + $datetime->getOffset();
        }

        return strftime($this->trans($format), $datetime->getTimestamp() + $offset);
    }
597482
public static function datetime($time = null, $nowIfNull = true, $format = 'Y-m-d H:i:s')
    {
        if (empty($time)) {
            return $nowIfNull ? date($format) : null;
        } else {
            return date($format, $time);
        }
    }
953090
function SQLDate($fmt, $col=false)
	{
	// use right() and replace() ?
		if (!$col) $col = $this->sysDate;

		/* use TO_CHAR() if $fmt is TO_CHAR() allowed fmt */
		if ($fmt== 'Y-m-d H:i:s')
			return 'TO_CHAR('.$col.", 'YYYY-MM-DD HH24:MI:SS')";

		$s = '';

		$len = strlen($fmt);
		for ($i=0; $i < $len; $i++) {
			if ($s) $s .= $this->concat_operator;
			$ch = $fmt[$i];
			switch($ch) {
			case 'Y':
			case 'y':
				if ($len==1) return "year($col)";
				$s .= "char(year($col))";
				break;
			case 'M':
				if ($len==1) return "monthname($col)";
				$s .= "substr(monthname($col),1,3)";
				break;
			case 'm':
				if ($len==1) return "month($col)";
				$s .= "right(digits(month($col)),2)";
				break;
			case 'D':
			case 'd':
				if ($len==1) return "day($col)";
				$s .= "right(digits(day($col)),2)";
				break;
			case 'H':
			case 'h':
				if ($len==1) return "hour($col)";
				if ($col != $this->sysDate) $s .= "right(digits(hour($col)),2)";
				else $s .= "''";
				break;
			case 'i':
			case 'I':
				if ($len==1) return "minute($col)";
				if ($col != $this->sysDate)
					$s .= "right(digits(minute($col)),2)";
					else $s .= "''";
				break;
			case 'S':
			case 's':
				if ($len==1) return "second($col)";
				if ($col != $this->sysDate)
					$s .= "right(digits(second($col)),2)";
				else $s .= "''";
				break;
			default:
				if ($ch == '\\') {
					$i++;
					$ch = substr($fmt,$i,1);
				}
				$s .= $this->qstr($ch);
			}
		}
		return $s;
	}
785692
function _date_time_string( $datetime, $parno=FALSE ) {
    $datetime = (string) trim( $datetime );
    $tz  = null;
    $len = strlen( $datetime ) - 1;
    if( 'Z' == substr( $datetime, -1 )) {
      $tz = 'Z';
      $datetime = trim( substr( $datetime, 0, $len ));
    }
    elseif( ( ctype_digit( substr( $datetime, -2, 2 ))) && // time or date
                  ( '-' == substr( $datetime, -3, 1 )) ||
                  ( ':' == substr( $datetime, -3, 1 )) ||
                  ( '.' == substr( $datetime, -3, 1 ))) {
      $continue = TRUE;
    }
    elseif( ( ctype_digit( substr( $datetime, -4, 4 ))) && // 4 pos offset
            ( ' +' == substr( $datetime, -6, 2 )) ||
            ( ' -' == substr( $datetime, -6, 2 ))) {
      $tz = substr( $datetime, -5, 5 );
      $datetime = substr( $datetime, 0, ($len - 5));
    }
    elseif( ( ctype_digit( substr( $datetime, -6, 6 ))) && // 6 pos offset
            ( ' +' == substr( $datetime, -8, 2 )) ||
            ( ' -' == substr( $datetime, -8, 2 ))) {
      $tz = substr( $datetime, -7, 7 );
      $datetime = substr( $datetime, 0, ($len - 7));
    }
    elseif( ( 6 < $len ) && ( ctype_digit( substr( $datetime, -6, 6 )))) {
      $continue = TRUE;
    }
    elseif( 'T' ==  substr( $datetime, -7, 1 )) {
      $continue = TRUE;
    }
    else {
       //echo "_date_time_string 1:  $datetime tz=$tz.<br />\n"; // test ###
      $cx  = $tx = 0;    //  19970415T133000 US-Eastern
      for( $cx = -1; $cx > ( 9 - $len ); $cx-- ) {
        if(( ' ' == substr( $datetime, $cx, 1 )) || ctype_digit( substr( $datetime, $cx, 1 )))
          break; // if exists, tz ends here.. . ?
        elseif( ctype_alpha( substr( $datetime, $cx, 1 )) ||
             ( in_array( substr( $datetime, $cx, 1 ), array( '-', '/' ))))
          $tx--; // tz length counter
      }
      if( 0 > $tx ) {
        $tz = substr( $datetime, $tx );
        $datetime = trim( substr( $datetime, 0, $len + $tx + 1 ));
      }
    }
      //echo "_date_time_string 1a:  $datetime tz=$tz.<br />\n"; // test ###
    if( 0 < substr_count( $datetime, '-' )) {
      $datetime = str_replace( '-', '/', $datetime );
      //echo "_date_time_string 1b:  $datetime tz=$tz.<br />\n"; // test ###
    }
    elseif( ctype_digit( substr( $datetime, 0, 8 )) &&
           ( 'T' ==      substr( $datetime, 8, 1 )) &&
            ctype_digit( substr( $datetime, 9, 6 ))) {
      $datetime = substr( $datetime,  4, 2 )
             .'/'.substr( $datetime,  6, 2 )
             .'/'.substr( $datetime,  0, 4 )
             .' '.substr( $datetime,  9, 2 )
             .':'.substr( $datetime, 11, 2 )
             .':'.substr( $datetime, 13);
      // $datetime = substr( $datetime, 0, 8 ).substr( $datetime, 9 ); // not PHP 4.x compatible
      //echo "_date_time_string 1c:  $datetime tz=$tz.<br />\n"; // test ###
    }
    $datestring = date( 'Y-m-d H:i:s', strtotime( $datetime ));
     // echo "_date_time_string 2:  $datestring tz=$tz. datetime=$datetime<br />\n"; // test ###
    $tz                = trim( $tz );
    $output            = array();
    $output['year']    = substr( $datestring, 0, 4 );
    $output['month']   = substr( $datestring, 5, 2 );
    $output['day']     = substr( $datestring, 8, 2 );
    if(( 6 == $parno ) || ( 7 == $parno )) {
      $output['hour']  = substr( $datestring, 11, 2 );
      $output['min']   = substr( $datestring, 14, 2 );
      $output['sec']   = substr( $datestring, 17, 2 );
      if( !empty( $tz ))
        $output['tz']  = $tz;
    }
    elseif( 3 != $parno ) {
      if(( '00' < substr( $datestring, 11, 2 )) ||
         ( '00' < substr( $datestring, 14, 2 )) ||
         ( '00' < substr( $datestring, 17, 2 ))) {
        $output['hour']  = substr( $datestring, 11, 2 );
        $output['min']   = substr( $datestring, 14, 2 );
        $output['sec']   = substr( $datestring, 17, 2 );
      }
      if( !empty( $tz ))
        $output['tz']  = $tz;
    }
    return $output;
  }