如何使用日期偏移来计算日期并同时固定其时间到特定时间?

huangapple go评论49阅读模式
英文:

How I can calculate a datetime using a day offest and fix its time into specific one at the same time?

问题

在我的情况下,我有一个表记录,其中有2列:

  • day_offset
  • time

我想按照以下方式计算日期时间:

  1. 将当前时间提前day_offset天
  2. 并将时间更改为数据库中的时间

例如,如果当前日期时间是2023-01-12 12:00:00,并且:

  1. day_offset为1
  2. time为13:00:00

我想要计算的日期时间/时间戳是:2023-01-11 13:00:00

我如何在PHP中实现这个?我需要使用这种方式进行,因为用户只能提供这些信息,我需要计算出一个到期时间戳。

英文:

In my case inside a table record I have 2 columns:

  • day_offset
  • time

And I want to calculate a datetime as follows:

  1. Move the current time day_offset days to the past
  2. And change the time into database

For example if the current datetime is 2023-01-12 12:00:00 and the:

  1. day_offset is 1
  2. time is 13:00:00

The calculated datetime/timestamp I want to be: 2023-01-11 13:00:00.

How I can do using PHP?

I need to do using this way because the user can only provide this info and I need to calculate an expiration timestamp out of this.

答案1

得分: 0

这是您提供的PHP代码示例,您希望将其翻译成中文:

一种方法是:

// 设置日期偏移
$day_offset = 1;
$time = '13:00:00';

$datetime = new Datetime();
$datetime->modify('-' . (int)$day_offset . ' 天');

// 创建新的日期时间对象
$datetime = Datetime::createFromFormat("Y-m-d H:i:s", $datetime->format('Y-m-d') . ' ' . $time);

我所做的是将其分为两个部分:

  1. 使用PHP的Datetime modify 修改日期
  2. 使用字符串连接重新创建新的日期时间。

如果时间不是字符串格式,可以使用以下代码:

$day_offset = 1;
$time = [
    'hour' => 13,
    'minute' => 0,
    'seconds' => 0
];

$datetime = new Datetime();
$datetime->modify('-' . (int)$day_offset . ' 天');
$datetime->setTime($time['hour'], $time['minute'], $time['seconds'], 0);

如果使用Carbon库,也可以采取相同的方法:

$day_offset = 1;
$time = '13:00:00';

$datetime = new Carbon();
$datetime->modify('-' . (int)$day_offset . ' 天');

// 创建新的Carbon对象
$datetime = Carbon::createFromFormat("Y-m-d H:i:s", $datetime->format('Y-m-d') . ' ' . $time);

或者,如果时间编码为整数,可以使用以下代码:

$day_offset = 1;
$time = [
    'hour' => 13,
    'minute' => 0,
    'seconds' => 0
];

$datetime = new Carbon();
$datetime->modify('-' . (int)$day_offset . ' 天');
$datetime->setTime($time['hour'], $time['minute'], $time['seconds'], 0);

请注意,以上代码是用于操作日期和时间的示例。

英文:

A way to do it is:

$day_offset = 1;
$time='13:00:00'

$datetime = new Datetime();
$datetime->modify('-'.(int)$day_offset.' days');

$datetime = Datetime::createFromFormat("Y-m-d H:i:s", $datetime->format('Y-m-d').' '.$time);

What I do is to brake it into 2 parts:

  1. Modifying the date using php's Datetime modify
  2. Re-create a new datetime using string concatenation.

If time is not in String format can use:

$day_offset = 1;
$time=[
 'hour'=>13,
 'minute'=>0,
 'seconds'=>0
]

$datetime = new Datetime();
$datetime->modify('-'.(int)$day_offset.' days');
$datetime->setTime($time['hour'],$time['minute'],$time['seconds'],0);

If using Carbon same think can happen as well:

$day_offset = 1;
$time='13:00:00'

$datetime = new Carbon();
$datetime->modify('-'.(int)$day_offset.' days');

$datetime = Carbon::createFromFormat("Y-m-d H:i:s", $datetime->format('Y-m-d').' '.$time);

Ot time encoded as Integer

$day_offset = 1;
$time=[
 'hour'=>13,
 'minute'=>0,
 'seconds'=>0
]

$datetime = new Carbon();
$datetime->modify('-'.(int)$day_offset.' days');
$datetime->setTime($time['hour'],$time['minute'],$time['seconds'],0);

huangapple
  • 本文由 发表于 2023年5月11日 19:29:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76227140.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定