Solved

Can't add hours to date and time stored in variable

Posted on 2016-09-25
8
14 Views
Last Modified: 2016-09-30
if I do this:

echo date("y-m-d h:i:sa", strtotime('+24 hour'));

Open in new window


it works as expected. But I am trying to add 24 hours to the date and time stored in the database which isn't working.

I have tried something like:

$register_date = $row['register_date'];
echo $register_date, strtotime('+24 hour');

Open in new window


but that just shows me the registered date from the database and a unix timestamp.

I am pretty stumped on this one.


-OR-

Should I just use an integer instead of date/time? The user has 24 hours to activate their account otherwise they will need to generate a new code.
0
Comment
Question by:Black Sulfur
  • 4
  • 4
8 Comments
 
LVL 35

Expert Comment

by:Terry Woods
ID: 41814593
You could probably do it in the database... are you using MySQL?
0
 
LVL 35

Assisted Solution

by:Terry Woods
Terry Woods earned 500 total points
ID: 41814594
If so, this might work:

SELECT DATE_ADD(register_date, INTERVAL 24 HOUR) 
FROM my_table;

Open in new window

0
 

Author Comment

by:Black Sulfur
ID: 41814597
Hi Terry,

I am using mysql and PHP. My select statement currently looks like this (prepared)

$stmt = $link->prepare("SELECT `register_date` FROM `db_users` WHERE `user_email` = ?");

Open in new window

0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Black Sulfur
ID: 41814608
So, I changed it to:

$stmt = $link->prepare("SELECT DATE_FORMAT(register_date, '%e-%m-%y %r') AS test FROM `db_users` WHERE `user_email` = ?");

Open in new window


which looks exactly the same as
$date_now = date("d-m-y h:i:sa");

Open in new window


The only difference is that one has pm in lower case and the other in uppercase PM, for some reason.
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 41814944
Did you try DATE_ADD as I suggested above?
0
 

Accepted Solution

by:
Black Sulfur earned 0 total points
ID: 41814949
It was getting a bit confusing with the prepared statements. I found a solution though which makes working with times and dates much easier and I think I got it now. I used Carbon which is awesome!

             
  $db_date = $row['register_date'];
		$carbon = new Carbon($db_date);
		$expiry_date = $carbon->copy()->addDays(1);
		$now = Carbon::now();

                         if($now > $expiry_date) {
			
			     header("location:expired.php");
		                  }

Open in new window


http://carbon.nesbot.com/docs/
0
 
LVL 35

Expert Comment

by:Terry Woods
ID: 41814976
Nice work! Perhaps you can accept that as the solution, with mine as Assisted.
0
 

Author Closing Comment

by:Black Sulfur
ID: 41823172
Found a solution using Carbon through my own research
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Moving from Mcrypt to OpenSSL 18 47
Custom Wordpress Loop 22 39
How to set the Tinymce Editor image path 4 23
jQuery Toggle & Anchor Links 5 43
Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question