Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 171
  • Last Modified:

Weird happenings in mktotime

I'm trying to convert a MySQL DATETIME string to mktime so I can shut an online web system once it reaches a predetermined hour on a specified day.

From MySQL I'm using mktime($closingdatetime) which returns the same timestamp as the current date time from getdate();

I've also tried the script from Convert MySQL DATETIME to Timestamp in PHP but it's timestamp is OLDER than the one given from today. Even if I set it a day or month into the future from MySQL.

This is the output from the script below (echo command)
1320962400 2011-11-11 11:00:00
1325484957 2011-10-11 11:15:57

Any ideas?

function mysql2timestamp($datetime){
	$val = explode(" ",$datetime);
	$date = explode("-",$val[0]);
	$time = explode(":",$val[1]);
	return mktime($time[0],$time[1],$time[2],$date[1],$date[2],$date[0]);


$ctd = getdate();

$Pyear = $ctd["year"];
$Byear = $Pyear +1;

$todayFull = $ctd["year"] . "-" . $ctd["mon"] . "-" . $ctd["mday"] . " " . $ctd["hours"] . ":" . $ctd["minutes"] . ":" . $ctd["seconds"];

$todayFullMKT= mysql2timestamp($todayFull);

$BallotEndYSTT= mysql2timestamp($ballotEnd);

$BallotStartYSTT = mktime($ballotStart);

if ($todayMktime >= $BallotEndYSTT) { $closedsite= 1; }
//if ($todayde >= $BallotEndYday) { $closedsite= 1; }

if ($todayMktime < $BallotStartYSTT) { $closedsite= 1; }

echo "$BallotEndYSTT $ballotEnd<br> $BallotStartYSTT $BallotStart<br> $todayMktime $todayFull" ;

Open in new window

  • 3
  • 3
  • 2
3 Solutions
Dave BaldwinFixer of ProblemsCommented:
If you use the "unix-timestamp" function in MySQL, http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_unix-timestamp , it returns an unsigned integer and that is an easier comparison than the ISO date/time format.
kiwistagAuthor Commented:
Thanks, any suggestions on how to call that back so it can be put into a PHP string? (sorry if it sounds a stupid question)

It's really weird how mktotime($todayFull) comes back fine but all the other ones called from MySQL don't even though the formatting is identical!
kiwistagAuthor Commented:
Although my re-coding from MySQL works for date & time
$BallotEndMktime = mktime($BallotEndHour,$BallotEndMinute,$BallotEndSecond,$BallotEndMonth,$BallotEndDay,$BallotEndYear);

Open in new window

Breaking today's date for example back onto a date string
$todayDATE = date("d-M-Y H:m",mktime($todayFull))

Open in new window

puts it as 12th Jan 2012 at 19:00
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Dave BaldwinFixer of ProblemsCommented:
Should be:

Ray PaseurCommented:
See this article.  I am pretty sure it has the design pattern in one of the examples.
Ray PaseurCommented:
To expand on that a little bit, here is what I would do.  It's really very easy and needs only a few lines of code.

Establish the future shutdown date and time.  Let's call it $shutdown.  Use date('c', strtotime($shutdown)) to normalize the value to an ISO-8601 DATETIME string.  Store that in in a small text file or in your data base in a column defined to be DATETIME.

On each  page load, get the current value of date('c') and compare it to the stored value.  If the current value is greater than the stored value, the web site is shut down.

Does that make sense? ~Ray
kiwistagAuthor Commented:
Sorta does, will give it a shot. Thanks for the help.
Ray PaseurCommented:
This is untested code, but it should demonstrate the principles...
$shutdown = date('c', strtotime('October 30, 2011 2:00pm'));
file_put_contents('shutdown.txt', $shutdown);

Open in new window

$current  = date('c');
$shutdown = file_get_contents('shutdown.txt');
if ($current > $shutdown) die("THIS SITE WAS SHUT DOWN AT $shutdown");

Open in new window


Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 3
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now