Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

php - Variable vs. Text Subtraction

  • 22-01-2003 12:07PM
    #1
    Registered Users, Registered Users 2 Posts: 1,068 ✭✭✭


    I've just started doing some simple php, and want to know is it possible to subtract a "text-value" (for want of a better term) from a variable, i.e.

    <td><center>31/1</center></td>

    I have the date 31st of Janurary in a table column (as above), and I have another variable called "$mday" ( from getdate() ) which holds today's date in numerical format, e.g. 22

    So, if I declare a new variable "$total", how do I initalise it do subtract $mday from 31? Or is that even possible?

    If it's not possible, no problem, as I can just declare 31 as a variable, but I'm curious now just to see if this can be done :)


Comments

  • Registered Users, Registered Users 2 Posts: 1,709 ✭✭✭Balfa


    i don't quite get what you want to do.

    if you want to initialise $total as 31 - $mday, wouldn't you just do $total = 31 - $mday?

    *boggle* :/


  • Registered Users, Registered Users 2 Posts: 1,068 ✭✭✭Magic Monkey


    Yes, thanks, that actually makes more sense to use now, d'oh! :)

    But what I meant originally was could you point to the table column that contains the text-value instead of explicitly typing it, e.g.

    $total = $name - $mday
    Where "$name" would be the name of the table column (um, that is, if you could name it).

    You know, I'm probably just making a mountain out of a molehill here :p


  • Registered Users, Registered Users 2 Posts: 1,709 ✭✭✭Balfa


    i'm not sure if you can do _that_, but you could define that as another variable, so you can have something like:

    $tabledate = 31
    <td><center><? $tabledate ?>/1</center></td>
    // the above line will insert the value for tabledate in
    // where you manually typed 31 before
    $total = $tabledate - $mday


    that any help?


  • Registered Users, Registered Users 2 Posts: 1,068 ✭✭✭Magic Monkey


    Originally posted by Balfa
    i'm not sure if you can do _that_, but you could define that as another variable, so you can have something like:

    $tabledate = 31
    <td><center><? $tabledate ?>/1</center></td>
    // the above line will insert the value for tabledate in
    // where you manually typed 31 before
    $total = $tabledate - $mday


    that any help?

    Heh, funnily enough that's what I ended up doing just a few minutes ago :D
    Except that the $tabledate is "31/1" in my code, and when I do $tabledate - $mday (22) I get 9, which is right. It must ignore the /1 for some reason.

    While you're here, you don't know anything about date subtraction, do you? ;)


  • Closed Accounts Posts: 19,777 ✭✭✭✭The Corinthian


    Originally posted by Magic Monkey
    While you're here, you don't know anything about date subtraction, do you? ;)
    You may find the mktime useful for that. Otherwise I would reccomend you avoid using datetime types such as MySQL DATETIME and use only UNIX timestamps.

    Beyond that, you should RTFM on PHP Date/Time functions. Hope this helped.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 1,068 ✭✭✭Magic Monkey


    Originally posted by The Corinthian
    You may find the mktime useful for that. Otherwise I would reccomend you avoid using datetime types such as MySQL DATETIME and use only UNIX timestamps.

    Beyond that, you should RTFM on PHP Date/Time functions. Hope this helped.

    Thanks Corinthian,

    I found a good tutorial here, that I'm reading ATM. Yeah, DATETIME wouldn't be appropriate in this case, and time() seems to be extremley flexible, so it's all cool.


Advertisement