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!
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);
Leave away the unnecessary zeroes, that should do the trick.
mktime(23, 50, 0, 9, 5, intval(date("Y")));
Related
So I have my code that fetches the date of the first of the next month at midnight:
$future_date = date("Y-m-d H:i:s", mktime(0, 0, 0, date("m")+1, 1, date("Y")));
What I can't figure out (and I've Googled a fair bit) is how to count the seconds from NOW until that future date.
Storing mktime() into $future_date_unix means you can use it for both the string generation you require and the seconds calculation using a subtraction of the current time() value.
$future_date_unix = mktime( 0, 0, 0, date('m') + 1, 1, date('Y') );
$future_date = date( 'Y-m-d H:i:s', $future_date_unix );
$seconds_till_future_date = $future_date_unix - time();
I've got a little doubt.
How I can know the timestamp from day X, about hour 00:00:00 till 23:59:59?
I guess that time() just gives you the current timestamp, and I would need the timestamp from the beginning of a day to the final one so I could check my DB in MySQL for a moment between those times.
strtotime converts date from string to timestamp.
$start_date = "2012-01-10";
$end_date = "2012-04-14";
$start = strtotime($start_date . " 00:00:00");
$end = strtotime($end_date . " 23:59:59");
$today = new DateTime();
$morning = $today->format('Y-m-d 00:00:00');
$evening = $today->format('Y-m-d 23:59:59');
You could use mktime().
http://php.net/manual/en/function.mktime.php
// Set the default timezone to use. Available as of PHP 5.1
date_default_timezone_set('UTC');
// Prints: July 1, 2000 is on a Saturday
echo "July 1, 2000 is on a " . date("l", mktime(0, 0, 0, 7, 1, 2000));
// Prints something like: 2006-04-05T01:02:03+00:00
echo date('c', mktime(1, 2, 3, 4, 5, 2006));
Have a look at the strtotime function, e.g.
$date1 = 'today 00:00:00';
$date2 = 'today 23:59:59';
$unixTime1 = strtotime($date1);
$unixTime2 = strtotime($date2);
Comment is the output I'm seeing. Help!
echo date("Y-m-t", strtotime("2012-07-31 -1 month")); //2012-07-31
echo date("Y-m-t", strtotime("2012-07-31 -2 month")); //2012-05-31
July has 31 days, so it can't be processing it as August 1, right? Even if it were, the second line should work. Right?!
Thanks!
What's happening here is, "7-31" minus one month comes out as "6-31", which translates to "7-1", which - asking for the last day of the month - comes out as "7-31".
When you want to do month math, it's almost always, if not always, better to build the time using mktime.
$month = 7;
echo date("Y-m-t", mktime(0, 0, 0, $month - 1, 1, 2012)); //2012-06-30
Note that when doing month math in mktime, always give '1' as the day. Or really anything as long as it's 28 or lower.
You can do something like this:
$month = date('m') ;
$day = date('d') ;
$year = date('Y') ;
// ----
$current = mktime( 0, 0, 0, $month , $day, $year);
$yesterday = mktime( 0, 0, 0, $month , $day-1, $year);
$tomorrow = mktime( 0, 0, 0, $month , $day+1, $year);
// ----
echo '<pre>Current Day: '.date('Y-m-d', $current).'</pre>';
echo '<pre>Day Before: '.date('Y-m-d', $yesterday).'</pre>';
echo '<pre>Day After: '.date('Y-m-d', $tomorrow).'</pre>';
If you're looking for the last day of July:
$july = mktime( 0, 0, 0, 8, 1-1, 2012);
// Last Day of July: 2012-07-31
echo '<pre>Last Day of July: '.date('Y-m-d', $july).'</pre>';
Using mktime, just enter the first day of August and subtract one day.
Trying to get the ISO-8601 numeric representation of the day of the week ("N") in PHP using the date() function; however, it keeps returning "3" no matter what day I use with mktime().
<?php
$date = date( "Y-m-d H:i:s", mktime(0, 0, 0, 9, 16, 2011) );
//$date = date( "Y-m-d H:i:s", mktime(0, 0, 0, 9, 17, 2011) );
print_r(date('N', $date));
?>
Output: 3
You shouldn't feed a date string into the second argument for date(), it should be an integer containing the Unix timestamp (the value returned from mktime()). See the date() documentation.
$date = mktime(0, 0, 0, 9, 16, 2011);
var_dump(date('N', $date)); // string(1) "5"
With your original code:
$date = date( "Y-m-d H:i:s", mktime(0, 0, 0, 9, 16, 2011) );
print_r(date('N', $date));
The value of $date is "2011-09-16 00:00:00". This is not an integer, and certainly not the Unix timestamp for that date/time; because of that, date() cannot work with the value and reverts back to using the Unix epoch (0 timestamp) which is 1 Jan 1970. Also, an E_NOTICE message stating "A non well formed numeric value encountered in [file] on line [line]" is issued.
PHP is trying to interpret the string generated by date( "Y-m-d H:i:s", mktime(0, 0, 0, 9, 16, 2011) ); as a date, but PHP uses seconds since epoch as a datatime. You can just pass the result of mktime into the second data function call like this:
$dateTime = mktime(0, 0, 0, 9, 16, 2011)
$date = date("Y-m-d H:i:s", $dateTime);
echo date('N', $dateTime);
// results in "5"
I have this code, and it prints out as '01/01/1970'
$dob = mktime(0, 0, 0, date("m"), date("d")-1, date("y")-18);
echo "DOB is ".date("d/m/y", $dob);
Why is the year not 18 years less than today?
date("y") == 11 and 11-18 == -7. You need date("Y") == 2011.
Debugging tip: Print out separate parts of the code so you see what's going on. echo $dob shows that the problem is on the first line, and echo date("y")-18 tells that it's the last argument to mktime() that causes it.
This is the easiest solution :
$dob = strtotime('-18 years');
echo date('d/m/y', $dob);
strtotime() is a powerful function.
try
$dob = mktime(0, 0, 0, date("m"), date("d")-1, date("Y")-18);
echo "DOB is ".date("d/m/y", $dob);
According to the manual, when you specify small y as the argument to date function, it will return two-digit representation of the current year. Since the current year is 2011, it will return 11. Subtracting 18 from it will give you a negative result, that's why mktime is resetting to the original timestamp.
Change date("y") to date("Y"), that is, replace small y with capital Y, then you will get the desired result.
The following code is imho much easier to read:
<?php
$dob = new DateTime();
printf("\nToday is %s", date_format($dob, 'D, M d Y'));
$dob->modify('-1 day');
$dob->modify('-18 year');
printf("\nToday minus %d day and %d year is %s",
-1, -18,
date_format($dob, 'D, M d Y'));
?>
Don't you agree? It is not so difficult to calculate date with PHP. Just look also at the PHP Date function for the various formats, such as Weeknumbers.