Category: PHP Development

PHP Development

Converting Numbers to Indian Number Format in PHP

In today’s globalized world, developers need to be able to format numbers according to different regional conventions. One such formatting requirement arises when dealing with Indian numerical notation. In this blog post, we’ll explore how to convert numbers into the Indian number format using PHP, along with a detailed explanation of the code provided. Understanding Indian Number Format: Before diving into the implementation, let’s briefly understand the Indian number format. In Indian numerical notation, thousands are separated by commas, and a dot represents decimals. For example, the number 1,00,000.50 represents one lakh (100,000) and fifty paise (0.50). The PHP Function: Here’s the PHP function convert_numbers_to_indian_format that converts a given number into the Indian number format: function convert_numbers_to_indian_format($number) { $formattedAmount = number_format($number); $decimal = (string)($number – floor($number)); $money = floor($number); $length = strlen($money); $delimiter = ''; $money = strrev($money); for($i=0;$i<$length;$i++){ if(( $i==3 || ($i>3 && ($i-1)%2==0) )&& $i!=$length){ $delimiter .=','; } $delimiter .=$money[$i]; } $formattedAmount = strrev($delimiter); $decimal = preg_replace("/0\./i", ".", $decimal); $decimal = substr($decimal, 0, 3); if( $decimal != '0'){ $formattedAmount = $formattedAmount.$decimal; } return $formattedAmount; } Code Explanation and Optimization: This function performs the following steps to convert a number into the Indian number format: Formats the whole number part using number_format. Extracts the decimal part and separates it from the whole number. Formats the whole number digits with commas as thousands separators. Concatenates the decimal part if non-zero, preserving up to three decimal places. Each code section is explained in detail, covering concepts such as string manipulation, iteration, and conditional logic. Additionally, optimization strategies are discussed to improve the efficiency and readability of the code. Usage Examples: Here are some examples demonstrating how to use the convert_numbers_to_indian_format function: echo convert_numbers_to_indian_format(100000); // Output: 1,00,000<br>echo show_indian_formatted_number(1234567.89); // Output: 12,34,567.89 These examples showcase the function’s ability to format numbers into the Indian number format, making it suitable for various applications. Conclusion: This blog post provides a comprehensive guide to converting numbers into the Indian number format using PHP. Developers can seamlessly handle numerical formatting requirements in their PHP projects by understanding the underlying principles and leveraging the provided function.Thank You!
PHP Development

How to send SMS using PHP

In this tutorial, we have explained how to send SMS using PHP. There are many SMS API service providers like Twilio, Nexmo, MSG91, Text local that you can use to send SMS using the PHP programming language. In this tutorial we learned how to send SMS using twilio API service provider. Twilio’s APIs (Application Programming Interfaces) power its platform for communications. Behind these APIs is a software layer connecting and optimizing communications networks around the world to allow your users to call and message anyone, globally. Twilio has a whole host of APIs, from SMS to Voice to Wireless! Step 1: Register for Twilio account Sign Up for free twilio account from here :  Step 2: Verify Phone Number Next, we need to create a phone number for your account from which you can send the SMS Click -> “Phone Numbers” in the sidebarClick -> “Get a number now” link to generate your own phone numberNow “Click -> Get your first Twilio phone number”.Once you click “Get your first Twilio phone number”, a new pop-up window will show your Twilio Phone Number. Now, click “Choose this Number”. Step 3: Create API Credentials Next, you need to get your account SID and your authorization token.On the Console Dashboard page you can get your credentials. Step 4: Install PHP SDK The method of installing the SDK is via composer
composer require twilio/sdk
The another method of installing the SDK without composer is possible to download and use the PHP SDK manually and unzip SDK folder into your project directory. You can download from here. Step 7: Create Integration
<?php

require __DIR__ . '/vendor/autoload.php'; //with composer
// based on where you downloaded and unzipped the SDK
require __DIR__ . '/twilio-php-main/src/Twilio/autoload.php'; //without composer
use Twilio\Rest\Client;

// Your Account SID and Auth Token from twilio.com/console
$account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
$auth_token = 'your_auth_token';

// In production, these should be environment variables. E.g.:
// $auth_token = $_ENV["TWILIO_AUTH_TOKEN"]

// A Twilio number you own with SMS capabilities
$twilio_number = "+15558675309";

$client = new Client($account_sid, $auth_token);
$client->messages->create(
    // Where to send a text message (your cell phone?)
    '+15017250604',
    array(
        'from' => $twilio_number,
        'body' => 'My first trial SMS!'
    )
);
Step 5: Receiving a SMS Here you go! Note: Trial accounts cannot send messages to unverified numbers; verify +9198XXX42887 at twilio.com/user/account/phone-numbers/verified, or purchase a Twilio number to send messages to unverified numbers. Conclusion: I hope this tutorial helpful for you, if you have any issue with integration, please comment below. Thank You!
Timezone
PHP Development

Dynamic Timezone : Same Time in Different Countries

In this tutorial we have explained how to create dynamic time zone which shows same time for different countries. You can use this feature for your online schedules to provide time to all users according to their country time.
<script type="text/javascript">

function dynamicTimeChange(timezone){

let date = new Date(Date.UTC(0, 0, 0, 13, 0, 0));
SlotTime = date.toLocaleString("en-US", {timeZone: timezone, hour: '2-digit', minute:'2-digit'});

document.getElementById("dynamictzDiv").innerHTML = SlotTime;
}
</script>
<label style="text-align: center;font-size: initial;">Select Your Timezone</label>
<?php
function select_Timezone($selected = '') {

// Create a list of timezone
$OptionsArray = timezone_identifiers_list();
$select= '<select name="SelectContacts" onchange="dynamicTimeChange(this.value);" id="selectTime" style="width: 120px;font-family: FontAwesome;">
<option disabled selected>
Please Select Timezone
</option>
<option value="Canada/Pacific">Canada/Pacific Standard Time</option>
<option value="Canada/Atlantic">Canada/Atlantic Standard Time</option>
<option value="Australia/West">Australian Central Western Standard Time&amp;lt;/option&amp;gt;
<option value="Australia/Queensland">Australia/Australian Eastern Standard Time</option>';

$select.='</select>';
return $select;
}
echo select_Timezone() . '<br>';
?>
<div class="timeDivPremiumSch" style="margin-top: 12px;">

<label id="dynamictzDiv">05:00 AM</label>

</div>
?>
Output: You can add Timezones in select list according to your needs. When you change the timezone from select dropdown list, it will change the time of label which is currently “05:00 AM” and display new time of your selected time. Here, I used “05:00 AM” as a static time so in this we can find your timezone’s “05:00 AM” of any other timezone. Date.UTC(0, 0, 0, 13, 0, 0) here, ’13’ is describing hour, you can add as your static time it will must be between 0-24. Conclusion: I hope this tutorial helpful for you, if you have any issue with this, please comment below. Thank You!
1
×