Solved

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

Posted on 2016-09-25
8
13 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
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 

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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

821 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