sábado, 19 de octubre de 2024

calculate_time_difference

 <?php


function calculate_time_difference($datetime_str) {

    // Split the input string into date and time components

    $datetime_parts = explode(' ', $datetime_str);

    

    // Extract the date and time

    $date_parts = explode('-', $datetime_parts[0]);

    $time_parts = explode(':', $datetime_parts[1]);

    

    // Normalize the date and time into integers for mktime

    $year = (int)$date_parts[0];

    $month = (int)$date_parts[1];

    $day = (int)$date_parts[2];

    

    $hour = (int)$time_parts[0];

    $minute = (int)$time_parts[1];

    $second = (int)$time_parts[2];

    

    // Create the target time using mktime

    $target_time = mktime($hour, $minute, $second, $month, $day, $year);

    

    // Get the current time

    $current_time = time();

    

    // Calculate the difference in seconds

    $diff = $current_time - $target_time;

    

    // Convert seconds into minutes and seconds

    $minutes = floor($diff / 60);    

    $seconds = $diff % 60;

    

    // Output the difference in "minutes:seconds" format

    echo "$minutes:$seconds\n";

    

    // Output the current date and time

    echo date("Y-m-d H:i:s");

}


// Example usage: Pass a date string in the "Y-m-d H:i:s" format

$datetime_str = "2024-10-20 01:45:16";

calculate_time_difference($datetime_str);


?>


viernes, 18 de octubre de 2024

convert to sec and mis

 <?php

function convert_to_mins($s){

    

$minutes= floor($s/60);    

$seconds=$s % 60;


echo "$minutes:$seconds";


    

    

}


function convert_to_secs($min){


$seconds= $min * 60;


echo "$seconds";

    

    

}



convert_to_mins(180);

echo "\n";

convert_to_secs(3);



sábado, 12 de octubre de 2024

filter array if value is >=70

 <?php

function pass($var){

    

  return $var>=70;

    

}


$notas=array("Jose"=>90,"Ambiorix"=>100,"Maria"=>65,90);

print_r(array_filter($notas,"pass"));


?>


Array

(

    [Jose] => 90

    [Ambiorix] => 100

    [0] => 90

)


domingo, 6 de octubre de 2024

MYSQL TRIGGERS EXAMPLE

 DELIMITER //


CREATE TRIGGER ProductSellPriceUpdateCheck 

    AFTER UPDATE  

    ON Products FOR EACH ROW  

BEGIN

IF NEW.SellPrice <= NEW.BuyPrice THEN

INSERT INTO Notifications(Notification,DateTime) 

VALUES(CONCAT(NEW.ProductID,' was updated with a SellPrice of ', NEW.SellPrice,' which is the same or less than the BuyPrice'), NOW()); 

    END IF;

END //




DELIMITER //


CREATE TRIGGER ProductSellPriceInsertCheck 

    AFTER INSERT  

    ON Products FOR EACH ROW  

BEGIN

IF NEW.SellPrice <= NEW.BuyPrice THEN

INSERT INTO Notifications(Notification,DateTime) 

VALUES(CONCAT('A SellPrice same or less than the BuyPrice was inserted for ProductID ', NEW.ProductID), NOW()); 

    END IF;

END //



DELIMITER //


CREATE TRIGGER NotifyProductDelete 

    AFTER DELETE   

    ON Products FOR EACH ROW   

INSERT INTO Notifications(Notification, DateTime) 

    VALUES(CONCAT('The product with a ProductID ', OLD.ProductID,' was deleted'), NOW()); 

END //

DELIMITER ;

sábado, 5 de octubre de 2024

MySQl official Guide

 https://dev.mysql.com/doc/refman/8.4/en/

https://developers.facebook.com/docs/pages-api/comments-mentions/

Get comments

To get the comments for a Page post, send a GET request to the /page_post_id/comments endpoint with the fields parameter set to a comma-separated list that includes the message field, to get the content for the comment and the from field, to get the Page-scoped ID (PSID) for the person or Page who commented on the post, if you would like to @mention the person or Page in the comment.

Example Request

Formatted for readability. Replace bold, italics values, such as page_post_id, with your values.
curl -i -X GET "https://graph.facebook.com/page_post_id/comments?fields=from,message"

On success, your app receives the following JSON response with the commentor's name, PSID, message and the comment ID:

{
  "data": [
    {
      "created_time": "2020-02-19T23:05:53+0000",
      "from": {
        "name": "commentor_name",
        "id": "commentor_PSID"
      },
      "message": "comment_content",
     "id": "comment_id"
    }
  ],
  "paging": {
    "cursors": {
      "before": "MQZDZD",
      "after": "MQZDZD"
    }
  } 

}  

https://developers.facebook.com/docs/pages-api/comments-mentions/

lunes, 27 de noviembre de 2023

Count Textarea Characters with JavaScript


I've been playing around with the Twitter API lately and, as you know, Tweets can only be 140 characters long. I wanted to use a textarea element, not an input type="text", so I couldn't just use maxchars to limit the number of characters. Additionally, I get annoyed when my text is chopped off when pasting. So, what to do? Well, I thought, why not make a little character counter like the one you find on the actual Twitter?

The first step is to create the JavaScript function. I placed mine in a file called count-chars.js and it looks like this:

function countChars(textbox, counter, max) {
  var count = max - document.getElementById(textbox).value.length;
  if (count < 0) { document.getElementById(counter).innerHTML = "<span style=\"color: red;\">" + count + "</span>"; }
  else { document.getElementById(counter).innerHTML = count; }
}

textbox and counter are the IDs of the elements of the textarea we're counting and the span where the count is going to go, respectively.

There's lots you can customize there. You could disable the form if too many characters are entered or you could automatically truncate the text. But, I prefer to just have the warning show up in red text.

The next step is to write the HTML itself:

<script type="text/javascript" src="/js/count-chars.js"></script>
<form action="#" method="POST">
<p>Tweet Something: <span id="char_count"></span><br><textarea name="tweet" id="textbox" class="form-control" rows="3" cols="60" onFocus="countChars('textbox','char_count',140)" onKeyDown="countChars('textbox','char_count',140)" onKeyUp="countChars('textbox','char_count',140)"></textarea></p>
<p><input type="submit" class="btn btn-primary" value="Tweet" /></p>
</form>