英文:
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
我想按照以下方式计算日期时间:
- 将当前时间提前day_offset天
- 并将时间更改为数据库中的时间
例如,如果当前日期时间是2023-01-12 12:00:00
,并且:
- day_offset为1
- 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:
- Move the current time day_offset days to the past
- And change the time into database
For example if the current datetime is 2023-01-12 12:00:00
and the:
- day_offset is 1
- 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);
我所做的是将其分为两个部分:
- 使用PHP的Datetime
modify
修改日期 - 使用字符串连接重新创建新的日期时间。
如果时间不是字符串格式,可以使用以下代码:
$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:
- Modifying the date using php's Datetime
modify
- 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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论