Jump to content

[SOLVED] Date/time calculations


Ninjakreborn

Recommended Posts

Originally I began accepting weird formats for dates.  Now I always take whatever format it is and convert it to a unix time stamp and put it in the database.  This was the original way that was suggested.  I found this very helpful based on multiple facts (I end up needing to change the format a lot and it's easy when it's in a time stamps, sometimes I need to do calculations and this format helps as well).  Well right now I am about to have some major calculations for a project.  These are going to be unlike any calculations I have ever done before, and it's going to take a lot of brain power to complete.  However I wanted to get the best environment to do these in.  I need to do stuff like take a certain time, and figure how long it'll be until different amounts of hours have passed.  For one I need to do something after 48 hours pass, for another I need to do something after 20 hours have passed (just some examples).

Right now I normally accept

04/07/2007 as the date, then I use strtotime on this.  From what I have here, can I use other date functions to still pull the time, and everything from the original date they entered.

Will I be able to get the time, and the hour, or how does this work inside PHP.

For example somebody puts in today's date, and I run it through str to time.

48 hours (2 days) from the time they entered it I need to run some calculations to change the status on whatever the payment was.  This is all fine, but now it leaves me with a question.

Since the original user only put in a date, it has no way of attributing a time to it.  Does it automatically start it at a specific time, or what?

I need to be able to specifically get a general idea before I start performing calculations on it, thanks.

 

Link to comment
Share on other sites

Ok, let me cut the question down a little so it's not as generic now that I know a little more of what I am needing to create.

I have a specific member id.

I have that, it's fine, to get it proper they'll be 2 halves to the id to make it a full id.

$id = $_SESSION['memberid'] . "second half";

The second half is what I am going to have problems with.

Basically I need to have the member id, followed by a 4 digit number.  This number is going to be all 0000 except for the id that was entered into the database.

I am getting the id into a variable called id using mysql_insert_id directly after the query.  That is fine, so now I have everything I need except how to do the calculations to make this proper.

1. I have the member id in a session.

2. I have the id for the entry into the database put into a variable.

Now I need to get that id of the database entry to basically add however many zero's "in front" of it to make it 4 digits.

If the number is 1 it needs to end up being 0001, if it's 2 it needs to end up being 0002.  If the number is 20 it needs to be 0020.  If it ends up being 200 it needs to be 0200.  If the number is 2000 it need's to just be 2000.  It needs to always be 4 digits in length, and whatever digits it does not have need to be appended at the beginning in the form of zero's.

Any advice on how to do this properly, thanks.

Link to comment
Share on other sites

Hmm, based on what you said and your example I formulated this

						if (mysql_query($insert)) {
						$id = mysql_insert_id();
						$memberid = $_SESSION['memberid'];
						$length = strlen($id);
						switch ($length) {
							case 1:
								$modid = "000" . $id;
								break;
							case 2:
								$modid = "00" . $id;
								break;
							case 3:
								$modid = "0" . $id;
								break;								
							case 4:
								$modid = $id;
								break;
							default:
								$modid = $id;
						}
						$transactionid = $memberid . $modid;
						echo $transactionid;

and it worked perfectly, thanks.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.