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
https://forums.phpfreaks.com/topic/129142-changing-the-date-format-explode/
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! :)

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.

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>";
?>

  • 4 weeks later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.