Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 22
  1. #1
    Regular Coder
    Join Date
    Feb 2010
    Posts
    209
    Thanks
    15
    Thanked 2 Times in 2 Posts

    Can someone convert my time and date to seconds?

    Hi,

    I have time and date variables:

    PHP Code:
    $date date("Y-m-d"); 
    $time date("H:i:s"); 
    Can someone give me a php script which would count and sum their both seconds.

    I would be really thankful for that
    Last edited by auriaks; 03-05-2010 at 06:48 PM.

  • #2
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    http://php.net/time
    Description
    int time ( void )

    Returns the current time measured in the number of seconds since the Unix Epoch (January 1 1970 00:00:00 GMT).
    Are you a Help Vampire?

  • #3
    Regular Coder
    Join Date
    Feb 2010
    Posts
    209
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by tomws View Post
    Some misunderstanding... I need seconds of time and date. Like today in seconds would be like 1021524521142 seconds

  • #4
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by auriaks View Post
    Some misunderstanding... I need seconds of time and date. Like today in seconds would be like 1021524521142 seconds
    Thats precisely what time() does and what date() will use when not provided a second argument. Time() is what will fit into you're current code.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #5
    Regular Coder
    Join Date
    Feb 2010
    Posts
    209
    Thanks
    15
    Thanked 2 Times in 2 Posts
    when I write echo "$time and $date"; it shows 20:38:12 and 2010-03-03

    What I have to write to get answer 1884546131548?

    It seems that i haven't understood something yet

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    You'll never get that number anyway, its too big for PHP to store in an integer.
    The largest number PHP can store is 2147483647, nearly twice that for display with unsigned %u currently.

    Perhaps you're thinking that time starts at year 0, but time starts at epoch December 31 1969 / January 1 1970.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Regular Coder
    Join Date
    Feb 2010
    Posts
    209
    Thanks
    15
    Thanked 2 Times in 2 Posts
    hmm, the thing is, that I want to compare saved time and date yesterday 2010-03-02 21:22:18 and today's time and date 2010-03-03 23:00:00

    PHP Code:
    if ((today time and date) <= (yesterday time and date 6 minutes)) {
    echo 
    "the time is same";
    } else {
    echo 
    "Go away! Your time is too old";

    how I can do this then?

  • #8
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    If you'd take the time to consult the documentation as linked in post #2, you'd be well on your way to solving the problem.
    Are you a Help Vampire?

  • #9
    Regular Coder
    Join Date
    Feb 2010
    Posts
    209
    Thanks
    15
    Thanked 2 Times in 2 Posts
    I dont know how to use this with time:

    PHP Code:
    date('Y-m-d'strtotime('+6 minutes')) 

  • #10
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Thats correct, you'll never see a difference though compared to no second parameter since 6 minutes will only effect the last 6 minutes of the day and you have not shown the time above.
    Use this to also display the time:
    PHP Code:
    print date('Y-m-d H:i:s'strtotime('+6 minutes')); 
    Edit:
    BTW, just noticed you had another post above. Comparisons of dates are always a pain. This is why using a pure PHP approach, I'd recommend always using integers as you're datatype for storing times. The date() function is useful for formatting the date from an integer timestamp, but strtotime itself is limited in what its allowed to parse. If you're using MySQL or another database engine, most modern ones can process dates using things like datediff and datesub/add. These let you do some comparisons between you're dates and not concern yourself with the limitations of the 32-bit integer (don't worry we still have 20 some odd years).
    Last edited by Fou-Lu; 03-04-2010 at 03:39 PM.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #11
    Regular Coder
    Join Date
    Feb 2010
    Posts
    209
    Thanks
    15
    Thanked 2 Times in 2 Posts
    it is possible to do something like that?

    PHP Code:
    $time = ((date('Y-m-d H:i:s'strtotime('-6 minutes')))-(date('Y-m-d H:i:s'strtotime('+6 minutes')))); 

  • #12
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Not unless you want to compare strings.
    You can however subtract the results of strtotime and then process it as a date.
    PHP Code:
    $time date('Y-m-d H:i:s', (strtotime('-6 minutes') - strtotime('+6 minutes'))); 
    strtotime itself can take a second parameter to indicate the offset result we're applying to. By default this is time(), but it can be a prestored timestamp if you want.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #13
    Regular Coder
    Join Date
    Feb 2010
    Posts
    209
    Thanks
    15
    Thanked 2 Times in 2 Posts
    Quote Originally Posted by Fou-Lu View Post
    Not unless you want to compare strings.
    Yes, thats what I want... I need to compare two times. All I have done now, I just compared two times like this:

    PHP Code:
    $time1
    $time2
    if ($time1 <> $time2) {you are off} else {you are on
    but what if I need exactly the difference of these two times? in minutes or seconds...

  • #14
    Senior Coder tomws's Avatar
    Join Date
    Nov 2007
    Location
    Arkansas
    Posts
    2,644
    Thanks
    29
    Thanked 330 Times in 326 Posts
    Quote Originally Posted by auriaks View Post
    Yes, thats what I want
    No, that's not what you want. You want to compare times. While strings can be used (compared) in very specific circumstances, you'd be better served by converting to timestamps (integers) and comparing those. Then it's just simple math.
    Are you a Help Vampire?

  • #15
    Regular Coder
    Join Date
    Feb 2010
    Posts
    209
    Thanks
    15
    Thanked 2 Times in 2 Posts
    how is timestamp() different from date() ?


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •