gpt4 book ai didi

php - 如何将时区解析到这个 php prettyprint 库中以根据设置的时区打印出时间?

转载 作者:行者123 更新时间:2023-11-29 06:22:18 24 4
gpt4 key购买 nike

我目前正在使用 https://github.com/danielstjules/php-pretty-datetime用于打印我的时间。目前它正在吐出 UTC 时间,因为那是 MYSQL 中的时间。我有一个 PrettyPrint.php 作为组件:

<?php

namespace Component;

use PrettyDateTime\PrettyDateTime,
Phalcon\Mvc\User\Component,
DateTime,
Component\User;

class PrettyPrint extends Component
{

public static function prettyPrint($dateTime, $refDateTime = 'now')
{
$user = new User();
date_default_timezone_set($user->getSessionUserTimeZone());
$getDateTimeObject = function($dateType) {
if (is_numeric($dateType)) { //Linux timestamp
$date = (new DateTime())->setTimestamp((int)$dateType);
} else if (is_string($dateType)) {
$date = new DateTime($dateType);
}
return $date;
};
$date = $getDateTimeObject($dateTime);
$refDate = $getDateTimeObject($refDateTime);
if (is_a($date, 'DateTime') && is_a($refDate, 'DateTime')) {
return PrettyDateTime::parse($date, $refDate);
}
}

public static function excerpt($string, $len = 10)
{
return substr($string, 0, $len);
}
}

我可以使用 $user->getSessionUserTimeZone() 获取用户的时区名称,例如“America/New-York”,但我不确定如何在 return PrettyDateTime 中解析它: :parse($date, $refDate);

这是来自 github 中 src 的 PrettyDateTime.php 并且解析函数在那里,但我不确定该怎么做:

<?php

namespace PrettyDateTime;

class PrettyDateTime
{
// The constants correspond to units of time in seconds
const MINUTE = 60;
const HOUR = 3600;
const DAY = 86400;
const WEEK = 604800;
const MONTH = 2628000;
const YEAR = 31536000;

/**
* A helper used by parse() to create the human readable strings. Given a
* positive difference, corresponding to a date in the past, it appends the
* word 'ago'. And given a negative difference, corresponding to a date in
* the future, it prepends the word 'In'. Also makes the unit of time plural
* if necessary.
*
* @param integer $difference The difference between dates in any unit
* @param string $unit The unit of time
* @return string The date in human readable format
*/
private static function prettyFormat($difference, $unit)
{
// $prepend is added to the start of the string if the supplied
// difference is greater than 0, and $append if less than
$prepend = ($difference < 0) ? 'In ' : '';
$append = ($difference > 0) ? ' ago' : '';

$difference = floor(abs($difference));

// If difference is plural, add an 's' to $unit
if ($difference > 1) {
$unit = $unit . 's';
}

return sprintf('%s%d %s%s', $prepend, $difference, $unit, $append);
}

/**
* Returns a pretty, or human readable string corresponding to the supplied
* $dateTime. If an optional secondary DateTime object is provided, it is
* used as the reference - otherwise the current time and date is used.
*
* Examples: 'Moments ago', 'Yesterday', 'In 2 years'
*
* @param DateTime $dateTime The DateTime to parse
* @param DateTime $reference (Optional) Defaults to the DateTime('now')
* @return string The date in human readable format
*/
public static function parse(\DateTime $dateTime, \DateTime $reference = null)
{
// If not provided, set $reference to the current DateTime
if (!$reference) {
$reference = new \DateTime(NULL, new \DateTimeZone($dateTime->getTimezone()->getName()));
}

// Get the difference between the current date and the supplied $dateTime
$difference = $reference->format('U') - $dateTime->format('U');
$absDiff = abs($difference);

// Get the date corresponding to the $dateTime
$date = $dateTime->format('Y/m/d');

// Throw exception if the difference is NaN
if (is_nan($difference)) {
throw new Exception('The difference between the DateTimes is NaN.');
}

// Today
if ($reference->format('Y/m/d') == $date) {
if (0 <= $difference && $absDiff < self::MINUTE) {
return 'Moments ago';
} elseif ($difference < 0 && $absDiff < self::MINUTE) {
return 'Seconds from now';
} elseif ($absDiff < self::HOUR) {
return self::prettyFormat($difference / self::MINUTE, 'minute');
} else {
return self::prettyFormat($difference / self::HOUR, 'hour');
}
}

$yesterday = clone $reference;
$yesterday->modify('- 1 day');

$tomorrow = clone $reference;
$tomorrow->modify('+ 1 day');

if ($yesterday->format('Y/m/d') == $date) {
return 'Yesterday';
} else if ($tomorrow->format('Y/m/d') == $date) {
return 'Tomorrow';
} else if ($absDiff / self::DAY <= 7) {
return self::prettyFormat($difference / self::DAY, 'day');
} else if ($absDiff / self::WEEK <= 5) {
return self::prettyFormat($difference / self::WEEK, 'week');
} else if ($absDiff / self::MONTH < 12) {
return self::prettyFormat($difference / self::MONTH, 'month');
}

// Over a year ago
return self::prettyFormat($difference / self::YEAR, 'year');
}
}

最佳答案

您将 DateTime 对象传递给此类,那么为什么不运行:

$date = new DateTime($inputDate, new DateTimeZone('UTC'));
$date->setTimezone(new DateTimeZone('New/Timezone'));

然后将 $date 传递给 prettyprint 类。


然后我在类里面阅读了更多内容并进行了思考。改变时区对这样的类(class)有什么影响吗?用户的时区是什么并不重要。 59 分钟前是任何地方的 59 分钟前(无论如何在地球上)。

关于php - 如何将时区解析到这个 php prettyprint 库中以根据设置的时区打印出时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32752134/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com