get strtotime of specific time in php
Asked Answered
G

2

7

I want to get the timestamp of a day/time for eg

17/12/2014 8pm

Currently I am doing

$curtime = strtotime(date("Y-m-d H:i:s"));

which is giving me the current timestamp but I need to be of 8pm.

Any help is highly appreciated. Thanks in advance.

Glendon answered 17/12, 2014 at 14:35 Comment(4)
Are you just trying to format it differently?Attune
All I want it to be in this format -- 1418823780Glendon
With your method, $curtime = strtotime(date("Y-m-d"). ' 20:00:00'); but it's not a very good method.Lananna
Thanks @Lananna let me tryGlendon
L
17

If you're trying to get a timestamp of today at 8pm, it's actually much more simple than using date since you can use relative times in a strtotime:

$curtime = strtotime('today 8pm');

If you test this with date:

echo date('Y-m-d H:i:s', $curtime); // Returns 2014-12-17 20:00:00

Here's a whole writeup on relative date formats that explains how you can construct proper relative dates.

Lananna answered 17/12, 2014 at 14:42 Comment(4)
Awesome...I just can't believe it can be that simple.Glendon
@Glendon Yeah! You can use some pretty common English wordings, like next Friday 8pm or yesterday 8pm. Or you can reduce the ambiguity and use +1 day 8pm for tomorrow at 8pm.Lananna
Something like english language :DGlendon
Thank goodness for this post. I was driving myself crazy trying things like 3pm today instead of today 3pm.Trinh
M
1

The best way to do this is using date_create_from_format. Checking out format's parameters, you will end up with something like this:

    $date = date_create_from_format('d/m/Y ga', '17/12/2014 8pm');
    if (!empty($date)) {//returns false if can't create date
        $timestamp = $date->getTimestamp();
        //echo date('d/m/Y H:i:s', $timestamp);
    }
Mayer answered 17/12, 2014 at 14:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.