Skip to content

Instantly share code, notes, and snippets.

@schuhwerk
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schuhwerk/a4d7ec37d829ae1d3dad to your computer and use it in GitHub Desktop.
Save schuhwerk/a4d7ec37d829ae1d3dad to your computer and use it in GitHub Desktop.
<style>body {word-wrap: break-word;line-height: 4px;}</style>
<?php
$myDb = array (
0 => "localhost", //host
1 => "root", //username
2 => "", //pass
3 => "citybike", //dbname
);
$mytable = 'table1';
$overwrite_existing = true;
set_time_limit ( 600 ); //10 minutes....
//http://www.learnersdictionary.com/qa/parts-of-the-day-early-morning-late-morning-etc
function dayranges ($hour) {
switch ($hour) {
case ($hour <= 7): return 'night';
case ($hour <= 5): return 'morning';
case ($hour <= 12): return 'afternoon';
case ($hour <= 17): return 'evening';
case ($hour <= 21): return 'night';
break;
}
}
function run(){
global $myDb, $mytable;
$mysqli = new mysqli($myDb[0],$myDb[1],$myDb[2],$myDb[3]);
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$insert_row = $mysqli->query("ALTER TABLE {$mytable} ADD id INT(12)AUTO_INCREMENT PRIMARY KEY");
$insert_row = $mysqli->query("ALTER TABLE {$mytable} ADD starttimeofday VARCHAR( 255 ) after id");
$insert_row = $mysqli->query("ALTER TABLE {$mytable} ADD starthour INT( 3 ) after id");
$sql = "SELECT id, starttime, starttimeofday, starthour FROM {$mytable} WHERE 1";
if ($result = $mysqli->query($sql)) {
while($row = $result->fetch_assoc()){
$subject = $row['starttime'];
//print_r($row);
if ($row['starttimeofday'] == '' || $overwrite_existing){
$pattern = '/ [0-9]*/';
preg_match($pattern, $subject, $matches);
//echo ($row['starttime']);
echo('.');
if (sizeof($matches[0])){ //check if there is a time...
//rtrim($matches, ":")
$timeofday = dayranges($matches[0]);
//echo ($timeofday);
$update = "UPDATE {$mytable} SET starthour = '{$matches[0]}' WHERE {$mytable}.id = '{$row['id']}'";
$insert = $mysqli->query($update);
//$update = "UPDATE {$mytable} SET starthour = '{$matches[0]}' WHERE {$mytable}.id = '{$row['id']}'";
$update = "UPDATE {$mytable} SET starttimeofday = '{$timeofday}' WHERE {$mytable}.id = '{$row['id']}'";
$insert = $mysqli->query($update);
//$insert->close();
}
}
else {
echo (',');
}
}
$result->close();
}
}
run();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment