2

I have a code but can't find where is an error. I have upcoming event and current time. If I convert my event to the timestamp it's less then current timestamp. May be you can help me.

My code below:

<?php

date_default_timezone_set('Etc/GMT');

$upcoming       = "2012.09.05 23:50";
$current        = time();

echo "Upcoming: " . $upcoming . " | Timestamp:" .  mktime(23, 50, 0, 09, 05, intval(date("Y")));
echo "<br>Current: " . time();
echo "<br>Current SIM: " .  mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y"));


?>

Will output:

Upcoming: 2012.09.05 23:50 | Timestamp:1323129000
Current: 1346855221
Current SIM: 1346855220

Where Current > Upcoming timestamp. (???) Thanks!

1
  • Yes. This string: date_default_timezone_set('Etc/GMT'); Commented Sep 5, 2012 at 14:42

2 Answers 2

7

Because you have 09 (with a preceding 0) this number is interpreted as an octal number, and so it's converted to 0. Use: mktime(23, 50, 0, 9, 5, intval(date("Y")));

You can explore this "feature" a bit;

var_dump(9);   // int 9
var_dump(09);  // int 0
var_dump(07);  // int 7
var_dump(17);  // int 17
var_dump(017); // int 15

EDIT;

date('n'); returns the month without leading zeros. And date('j'); and date('G'); return the day and hour without leading zeros. So you can change mktime(date("H"), date("i"), date("s"), date("m"), date("d"), date("Y")); to mktime(date("G"), date("i"), date("s"), date("n"), date("j"), date("Y"));

There's no way to get the number of minutes and seconds without leading zeros with date() so maybe you need to find another function for that.

EDIT:

To convert 2012.09.05 23:50 to a timestamp you can change the . to / and feed it to strtotime():

$str = '2012.09.05 23:50';
$str = str_replace(".", "/", $str);
$timestamp = strtotime($str);
Sign up to request clarification or add additional context in comments.

3 Comments

So how can I convert 09 to 9?
You have the 09 hardcoded in your code, so you can just change the code. Furthermore; date('n'); returns the month without leading zeros. And date('j'); returns the day without leading zeros
I can't use a date function as my string format is (2012.09.05 23:50) from .csv file. So I need to convert 09 and 05 to 9 and 5. Do you know how ca I do this? Thanks!
0

Leave away the unnecessary zeroes, that should do the trick.

mktime(23, 50, 0, 9, 5, intval(date("Y")));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.