Jump to content

changing the date format. explode()?


daydreamer

Recommended Posts

I have a date in this format

"2008-09-25 16:00:41";

 

and also a date in this format

"Thu Oct 16 22:07:22 BST 2008"

 

and i need to find out how much time has passed between each one in seconds. If two mintues have passed I need to do one thing, if not, then do another thing in my script.

 

The way im going to do this is using mktime() on each date, then 

$newestdate-$oldest

 

mktime takes input like this

 mktime(21, 38, 50, 10, 19, 2008)

  (hour, min, sec, month, day, year)

 

 

Anyway what is the best way to get my dates into the mktime format, and am i going the right way about this?

 

I was going to use explode(), but it only takes one delimiter as an argument.

Link to comment
Share on other sites

You can use strtotime()

 

// Returns a unix timestamp
$tsA = strtotime('2008-09-25 16:00:41');
$tsB = strtotime('Thu Oct 16 22:07:22 BST 2008');

$diff = $tsB - $tsA;
$diff_in_minutes = $diff / 60;

if( $diff_in_minutes > 2 )
{
     // chuck norris!
} 

 

Hope that's what you meant! :)

Link to comment
Share on other sites

yeh thats exactly what i meant cheers! lmao @ chuck norris. confused me for a second when i saw it.

 

anyway just tried it out, got a minor problem:

$date=strtotime("2008-09-25 16:00:41");

echo $date."<br>";
echo mktime(16, 00, 41, 09, 25, 2008)."<br>";

echo date('G, i, s, m, d, Y', mktime(16, 00, 41, 09, 25, 2008))."<br>";
echo date('G, i, s, m, d, Y', $date)."<br>";

 

output:

1222358441
1198598441
16, 00, 41, 12, 25, 2007
16, 00, 41, 09, 25, 2008

 

shouldent these be the same? I cant find where im going wrong.

Link to comment
Share on other sites

an number begining with 0, eg 07, is taken to be octal ( and 09 is illegal as octal and therefore upsetting things)

 

try

<?php
$date=strtotime("2008-09-25 16:00:41");

echo $date."<br>";
echo mktime(16, 0, 41, 9, 25, 2008)."<br>";

echo date('G, i, s, m, d, Y', mktime(16, 0, 41, 9, 25, 2008))."<br>";
echo date('G, i, s, m, d, Y', $date)."<br>";
?>

Link to comment
Share on other sites

  • 4 weeks later...
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.