How to add decimal month to some month in sql, php, perl, bash, sh?

Hello,

i`m looking for some way to add to some date an partial number of months, for example to 2015y 02m 27d + 2,54m

i need to write this script in php or bash or sh or mysql or perl in normal time o unix time

i`m asking or there are any simple way to add partial number of month to some other fixed date?

Welcome to the forums. Is this homework?

no, it`s for my web page

i tryed to do that in php in this way:

i keept in mind that all months have 31 days and after adding 2 moths ( in my example 2.54 ) ( never mind to wich date ) i calculated how much days i should take of ( for example if month 2 have 28 days and i keept in mind that all month have 31 days so for that month i must take - 3 days ) and after that i added thous 0.54 of that month days.

but its dificult to test all posibilities of errors and in some ways i get few days in answer bad.

i got errors when i was calculated end of months wich ones have 30;29;28 days

so in php my experianse is to week, but i practise bash sh languages, so i`m asking meaby there is any simple way to calculate it ( meaby use unix time ( meaby it have some extensions for it ) ) in bash sh ( if yes then ill simple from my web page will shell_exec to bash and ill take the values from it )

Please write your query clearly, try something like this just an example, remaining digits after decimal you have to convert to days and then you have to add .

$ php -r 'date_default_timezone_set("GMT"); echo date("Y-m-d H:i:s",strtotime("+1 months",strtotime(date("Y-m-d H:i:s"))))."\n" ;'

2014-07-02 12:51:07

can you give me exaple:

to date 2015-03-30 + 11 months

and

to date 2015-02-27 + 3.3725 months

thanks

I'm afraid you're heading a bit off target: there's an uncertainty to a month's length of 10% (3 days in 30), and you want to calculate a date to 500 ppm (which is some 20 min), starting from a date that has an uncertainty of 24h to it (assuming midnight of the day indicated).

1 Like

i just want to ask or there are any simple way to add to some date partial of month

for example to 2015-02-27 22:22:22 + 3.3429 of month

i searched google and i found only excel :slight_smile: my web page runs on linux mashie so i can calculate it on php, bash, sh, perl, ruby, and so on

i need it becouse i want to recalculate expiration date of some service that is sold to client

for example client have service wich one cost X/month, hes service expires on date yyyy-mm-dd hh:mm:ss and he want to change his service to diferent one wich one cost diferent price, so i need to get new expiration date, problem is that that each month have diferent number of days, so to calculate how much cost 1 second of his service is dificult.

btw sory for bad english

We're not worried about grammar errors in English if the message is clear., but there is confusion on how we know the length of a month. Could you show us what input & output you would expect and how you are hoping to generate your decimal that you wish to add.

  • Is the decimal in days or months?
  • Is it months.days or month.partmonth, e.g 3.3429 is three months and about 10 days (depending on which month)

We could help you write something that is not what you need, so more explanation with examples of what you expect the answer to be is better.

It's a bit like asking a child "What is 3+4x5=" They may answer 23 or 35. You need to be clear what you actually want and I'm sure we can work on something.

Thanks,
Robin

Some users have services (there are several different costs) and for example Thomas have bought "D3" service for 6 months. After some time he want to improve it to "D4". So now we're calculating his service virtual value (we know when he bought it and when it's ending), we have that there are left 0,5462 value of his paid virtual money. For example at beginning he paid 100 Eur, so there is 54.62 Eur left. "D4" for one month costs 25 Eur. We get: 54,62 / 25 = 2,1848 months. So now we need to add this time 2,1848 months (in minutes accuracy) to current date/time, (but it must consider, that each month has different number of days) and then we will get exact date that "D4" service will expire. Best way to get how many minutes it need to add to current time to get that expiration date. It can be done with php, bash or smth taht would work in linux.

How I can see there is no simple wat to get that.
So, to improve the proccess I can pay for this job by paypal. Please, help me and I will donate for you or forum.
It would be better get the code in php language, but also it can be done in any other language which works in linux.

Thank you for any help

Payment to speed up a response or escalate? Not for me. I'm just confused.

If you have a date, as you suggest and and you need to add 2.1848 months, can you show us what answer you want for a spread of months with various lengths, e.g.

  • 10th February (leap year)
  • 10th February (non-leap)
  • 10th March
  • 10th April

I/we need to know what your business rules are, then we can approach the issue. It would be dangerous for us to assume we know what your customer contracts state.

Robin

Are you sure you know what you/your business is talking about? Usually, a day is a good basic unit for licence payments, which you can count; I can't see the need to calculate to the split second. To make your customer happy, give him the fraction of a day that might be in question.

Agreed, "decimal fraction of a month" makes absolutely no sense no matter how you cut it.

We are not being mean. We are not denying you anything. We are not "holding out on you" for pay or anything else. The concept of "decimal fraction of a month" is incoherent and flawed.

Talk with them about it and get them to redefine it in days, or at least a fixed number of days.

Payment to the person who will finish the script (I need only to get exact expiration date). For example, if there is 2,1848 months, I need to get exact minutes count (2,1848 * 30 * 24 * 60) - 94383 minutes (but don't forget that each month has different number of days). Then mysql in our script will add that minutes ant will get exact expiration date of the service.

Yes, we can count that one month has 30 days, but if customer will buy for example for longer period of time he can loose several days. Also if there would be february with 28 days he will get +2 days for nothing. If they would change service a lot of time, this inaccuracy can become to weeks and etc. So we want to get exact date (if it's possible) depent on calendar

To "Pay", simply press the :b:Thanks button on the appropriate post(s). There is no other currency on this site.

So, if a month has 30 days whatever the actual length, it becomes a little more straightforward. So, the decimal number is number of months (and fractional bits to be days) to add to the current or a given date.

Before looking to code this, can you confirm that my logic is acceptable:-

  • Take the value to be added and multiply by 30 to get the number of days to add
  • Round fractions of days up or down (you will need to confirm the rules for this)
  • Add the days to the current date to give a the target date

Does that make sense?

I am confused, however, by your two statements:-

Do you want exact minutes based on always having 30 days in a month? That's making less sense, although it could be logical in some way I suppose.

Could we perhaps:-

  • Add the whole months[*]Consider the fractions as being always of 30 day months

What tools would you prefer to work in? There could be sh/ksh/bash, awk, perl, database query (you hint at the Oracle TO_DATE function) or many others. It's best for you if we work on something you are happy to support too.

Robin