2

I need to set timestamp eg. 4 hours ahead and 2 hours ahead separately

In my database, I have their columns as timestamp.

I know I could do something similar to this but am not sure if it's correct.

// For 4 hours ahead of time

$dt2 = date("Y-m-d 04:i:s");


//For 2 days ahead

$dt2 = date("Y-m-02 H:i:s");

4 Answers 4

2
//For 4 hours ahead of time
$dt2 = date("Y-m-d H:i:s", strtotime('+4 hours'));

//For 2 days ahead of time
$dt2 = date("Y-m-d H:i:s", strtotime('+2 days'));
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your response. it works now. Please what could be the best way to post for instance value 4 from form. I have try this $dt2 = date("Y-m-d H:i:s", strtotime('+.$_POST["value_4"] hours'));
Hi Sectona, to POST that value you need to create an html <form> element and set an <input> field equal to the value you want to POST. Its too much to explain in a comment box so if you need more help please post a new thread and I or someone else will help you.
Also, if you found my answer helpful please mark it as your accepted answer, as this helps me build my reputation on the site and also shows other visitors what was the best answer to your problem :)
I will mark as soon as am done. Am working on it. mean while when i queried database for certain time interval am getting the wrong output. I dont know where the problem is whether from my code or datatype timestamp that I used. I have posted the code for review and when am done, i will tick it as correct. Thanks
2

In my mind it's much better to work with DateTime field and the DateTime class.

You have the ability so modify that objects very easily. For example:

$aktDate = new \DateTime();

Now you have the actual date and time in an object. If you want you can put a string insight the DateTime function so set your date manually.

$aktDate = new \DateTime('Y-m-d 04:i:s');

Not you can modify your dates if you want with the modify function.

in your case:

$pastDate = clone $aktDate;
$pastDate->modify('+2 days');

$futureDate = clone $aktDate;
$futureDate->modify('+4 days');

if($pastDate < $aktDate && $aktDate < $futureDate) {
    // do something
}

I like the DateTime function much more because it's readable and you can work directly with your DateTime fields from your MySQL database if you have such fields. You can write that example much shorter but so you have better readability.

Comments

0
$date = new DateTime('now');
$date->modify('+2 days');
echo $date->format('Y-m-d H:i:s');

$date = new DateTime('now');
$date->modify('+4 hours');
echo $date->format('Y-m-d H:i:s');

Comments

0

You need to use the strtotime() function (http://php.net/manual/en/function.strtotime.php).

For your examples:

//+2 hours<br>
strtotime("+2 hours");

// +2 days<br>
strtotime("+2 days")

Edit: for what you ask, about posted values, the syntax is like this:

strtotime("+2".$_POST['field_name']." days");

You can use hours/days/months/weeks/years and either + or -

1 Comment

Thanks for your response. it works now. Please what could be the best way to post for instance value 2 from form. I have try this $dt2 = strtotime('+.$_POST["value_2"] hours');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.