كود لحساب الساعات والدقائق التي مرت او ستكون النسخة الاولي
ممتاز! سأقوم بإنشاء شورت كود جديد لحساب الوقت (ساعات ودقائق) بنفس المنطق والتصميم. إليك الكود الكامل:
<?php
// ============================================================================
// TIME AGO/FROM NOW COUNTER - STANDALONE VERSION
// ============================================================================
// Register the shortcode
add_shortcode('time_ago_from_now', 'time_ago_from_now_handler');
function time_ago_from_now_handler($atts) {
// Set default attributes
$atts = shortcode_atts(array(
'hours' => '0',
'minutes' => '0',
'type' => 'ago', // ago or from_now
'language' => 'en'
), $atts);
// Get attributes
$hours = intval($atts['hours']);
$minutes = intval($atts['minutes']);
$type = $atts['type'];
$language = $atts['language'];
// Validate inputs
if ($hours < 0 || $minutes < 0) {
return '<div style="color: red; padding: 20px; text-align: center; border: 2px solid red; border-radius: 10px;">❌ Error: Hours and minutes cannot be negative.</div>';
}
if ($hours === 0 && $minutes === 0) {
return '<div style="color: red; padding: 20px; text-align: center; border: 2px solid red; border-radius: 10px;">❌ Error: Please provide at least one hour or minute.</div>';
}
try {
// Create current date object
$now = new DateTime();
$target = new DateTime();
// Calculate the target time based on type
if ($type === 'ago') {
// Subtract hours and minutes
if ($hours > 0) {
$target->modify("-{$hours} hours");
}
if ($minutes > 0) {
$target->modify("-{$minutes} minutes");
}
} else {
// Add hours and minutes
if ($hours > 0) {
$target->modify("+{$hours} hours");
}
if ($minutes > 0) {
$target->modify("+{$minutes} minutes");
}
}
// Calculate total minutes for additional calculations
$total_minutes = ($hours * 60) + $minutes;
$total_hours = $total_minutes / 60;
$total_days = $total_hours / 24;
$total_weeks = $total_days / 7;
$total_months = $total_days / 30.44;
// Get detailed date information for SEO
$target_day_of_week = $target->format('l');
$target_day_ordinal = $target->format('jS');
$target_month = $target->format('F');
$target_year = $target->format('Y');
$target_time_12h = $target->format('g:i A');
$target_time_24h = $target->format('H:i');
$full_target_date = $target->format('F j, Y');
$full_target_datetime = $target->format('F j, Y \a\t g:i A');
// Current time for reference
$current_time = $now->format('g:i A');
$current_date = $now->format('F j, Y');
$current_full_datetime = $now->format('F j, Y \a\t g:i A');
// Build display content based on language and type
if ($language === 'ar') {
// Arabic content
$days_translation = [
'Monday' => 'الاثنين',
'Tuesday' => 'الثلاثاء',
'Wednesday' => 'الأربعاء',
'Thursday' => 'الخميس',
'Friday' => 'الجمعة',
'Saturday' => 'السبت',
'Sunday' => 'الأحد'
];
$months_ar = [
'January' => 'يناير',
'February' => 'فبراير',
'March' => 'مارس',
'April' => 'أبريل',
'May' => 'مايو',
'June' => 'يونيو',
'July' => 'يوليو',
'August' => 'أغسطس',
'September' => 'سبتمبر',
'October' => 'أكتوبر',
'November' => 'نوفمبر',
'December' => 'ديسمبر'
];
$target_day_ar = $days_translation[$target_day_of_week];
$target_month_ar = $months_ar[$target_month];
$current_month_ar = $months_ar[$now->format('F')];
// Build time string in Arabic
$time_parts = [];
if ($hours > 0) {
$time_parts[] = $hours . " ساعة";
}
if ($minutes > 0) {
$time_parts[] = $minutes . " دقيقة";
}
$time_string = implode(' و ', $time_parts);
if ($type === 'ago') {
$title = "ما كان الوقت قبل {$time_string}؟";
$main_text = "قبل {$time_string} كان {$target_day_ar}، {$target_day_ordinal} {$target_month_ar}، {$target_year} الساعة {$target_time_12h}";
$natural_language = "قبل {$time_string} كان الوقت واليوم هو {$target_day_ar}، {$target_day_ordinal} {$target_month_ar}، {$target_year} الساعة {$target_time_12h}، بينما الوقت الحالي هو {$current_time} من يوم {$current_month_ar} {$now->format('j')}، {$target_year}.";
$additional_info = "هذا يعني أن {$full_target_datetime} كان قبل {$time_string} من الآن.";
} else {
$title = "ما سيكون الوقت بعد {$time_string}؟";
$main_text = "بعد {$time_string} سيكون {$target_day_ar}، {$target_day_ordinal} {$target_month_ar}، {$target_year} الساعة {$target_time_12h}";
$natural_language = "بعد {$time_string} سيكون الوقت واليوم هو {$target_day_ar}، {$target_day_ordinal} {$target_month_ar}، {$target_year} الساعة {$target_time_12h}، بينما الوقت الحالي هو {$current_time} من يوم {$current_month_ar} {$now->format('j')}، {$target_year}.";
$additional_info = "هذا يعني أن {$full_target_datetime} سيكون بعد {$time_string} من الآن.";
}
$call_to_action = "استخدم هذه الأداة لحساب الأوقات الماضية أو المستقبلية بدقة.";
$breakdown_title = "التفاصيل الإضافية:";
// Breakdown questions
$hours_text = "كم ساعة " . ($type === 'ago' ? 'مضت' : 'قادمة') . "؟";
$minutes_text = "كم دقيقة " . ($type === 'ago' ? 'مضت' : 'قادمة') . "؟";
$days_text = "كم يوم " . ($type === 'ago' ? 'مضى' : 'قادم') . "؟";
$weeks_text = "كم أسبوع " . ($type === 'ago' ? 'مضى' : 'قادم') . "؟";
$seconds_text = "كم ثانية " . ($type === 'ago' ? 'مضت' : 'قادمة') . "؟";
} else {
// English content
// Build time string in English
$time_parts = [];
if ($hours > 0) {
$time_parts[] = $hours . " hour" . ($hours > 1 ? 's' : '');
}
if ($minutes > 0) {
$time_parts[] = $minutes . " minute" . ($minutes > 1 ? 's' : '');
}
if (count($time_parts) === 2) {
$time_string = $time_parts[0] . " and " . $time_parts[1];
} else {
$time_string = $time_parts[0];
}
if ($type === 'ago') {
$title = "What Time Was It {$time_string} Ago?";
$main_text = "{$time_string} ago was {$target_day_of_week}, {$target_month} {$target_day_ordinal}, {$target_year} at {$target_time_12h}";
$natural_language = "{$time_string} ago the time and day was {$target_day_of_week}, {$target_month} {$target_day_ordinal}, {$target_year} at {$target_time_12h}, while the current time is {$current_time} on {$current_date}.";
$additional_info = "This means that {$full_target_datetime} was {$time_string} ago from now.";
} else {
$title = "What Time Will It Be {$time_string} From Now?";
$main_text = "{$time_string} from now will be {$target_day_of_week}, {$target_month} {$target_day_ordinal}, {$target_year} at {$target_time_12h}";
$natural_language = "{$time_string} from now the time and day will be {$target_day_of_week}, {$target_month} {$target_day_ordinal}, {$target_year} at {$target_time_12h}, while the current time is {$current_time} on {$current_date}.";
$additional_info = "This means that {$full_target_datetime} will be {$time_string} from now.";
}
$call_to_action = "Use this tool to calculate past or future times accurately.";
$breakdown_title = "Additional Breakdown:";
// Breakdown questions
$hours_text = "How many hours " . ($type === 'ago' ? 'ago' : 'from now') . "?";
$minutes_text = "How many minutes " . ($type === 'ago' ? 'ago' : 'from now') . "?";
$days_text = "How many days " . ($type === 'ago' ? 'ago' : 'from now') . "?";
$weeks_text = "How many weeks " . ($type === 'ago' ? 'ago' : 'from now') . "?";
$seconds_text = "How many seconds " . ($type === 'ago' ? 'ago' : 'from now') . "?";
}
// Build the output HTML
$output = "
<div style='
background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
color: white;
padding: 30px;
border-radius: 15px;
margin: 25px 0;
box-shadow: 0 10px 30px rgba(0,0,0,0.2);
font-family: Arial, sans-serif;
'>
<!-- Main Title -->
<h1 style='
margin: 0 0 20px 0;
text-align: center;
font-size: 28px;
color: white;
'>
{$title}
</h1>
<!-- Natural Language SEO Description -->
<div style='
background: rgba(255,255,255,0.1);
padding: 25px;
border-radius: 12px;
margin-bottom: 25px;
text-align: center;
font-size: 18px;
line-height: 1.6;
border: 1px solid rgba(255,255,255,0.2);
'>
<p style='margin: 0 0 15px 0;'><strong>{$natural_language}</strong></p>
<p style='margin: 0 0 15px 0;'>{$additional_info}</p>
<p style='margin: 0; opacity: 0.9;'>{$call_to_action}</p>
</div>
<!-- Main Result Display -->
<div style='text-align: center; margin: 30px 0;'>
<div style='
display: inline-block;
background: rgba(255,255,255,0.2);
color: white;
padding: 40px 50px;
border-radius: 20px;
backdrop-filter: blur(10px);
border: 2px solid rgba(255,255,255,0.3);
'>
<div style='
font-size: 42px;
font-weight: bold;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
line-height: 1.3;
'>
" . ($language === 'ar' ?
"{$target_day_ar}، {$target_day_ordinal} {$target_month_ar}، {$target_year}<br>الساعة {$target_time_12h}" :
"{$target_day_of_week}, {$target_month} {$target_day_ordinal}, {$target_year}<br>at {$target_time_12h}") . "
</div>
<div style='
font-size: 20px;
opacity: 0.9;
margin-top: 15px;
padding-top: 15px;
border-top: 1px solid rgba(255,255,255,0.2);
'>
" . ($type === 'ago' ?
($language === 'ar' ? "قبل {$time_string}" : "{$time_string} ago") :
($language === 'ar' ? "بعد {$time_string}" : "{$time_string} from now")) . "
</div>
</div>
</div>
<!-- Main Statistics Grid -->
<div style='
display: grid;
grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
gap: 15px;
margin: 25px 0;
'>
<div style='
background: rgba(255,255,255,0.15);
padding: 20px;
border-radius: 12px;
text-align: center;
backdrop-filter: blur(10px);
border: 1px solid rgba(255,255,255,0.2);
'>
<div style='font-size: 32px; font-weight: bold; margin-bottom: 8px;'>" . number_format($hours) . "</div>
<div style='font-size: 14px; opacity: 0.9;'>" . ($language === 'ar' ? 'ساعة' : 'Hours') . "</div>
</div>
<div style='
background: rgba(255,255,255,0.15);
padding: 20px;
border-radius: 12px;
text-align: center;
backdrop-filter: blur(10px);
border: 1px solid rgba(255,255,255,0.2);
'>
<div style='font-size: 32px; font-weight: bold; margin-bottom: 8px;'>" . number_format($minutes) . "</div>
<div style='font-size: 14px; opacity: 0.9;'>" . ($language === 'ar' ? 'دقيقة' : 'Minutes') . "</div>
</div>
<div style='
background: rgba(255,255,255,0.15);
padding: 20px;
border-radius: 12px;
text-align: center;
backdrop-filter: blur(10px);
border: 1px solid rgba(255,255,255,0.2);
'>
<div style='font-size: 32px; font-weight: bold; margin-bottom: 8px;'>" . number_format($total_minutes) . "</div>
<div style='font-size: 14px; opacity: 0.9;'>" . ($language === 'ar' ? 'دقيقة إجمالي' : 'Total Minutes') . "</div>
</div>
</div>
<!-- Detailed Breakdown for SEO -->
<div style='
background: rgba(0,0,0,0.2);
padding: 25px;
border-radius: 12px;
margin-top: 20px;
'>
<h3 style='
margin: 0 0 20px 0;
text-align: center;
color: white;
font-size: 22px;
'>
{$breakdown_title}
</h3>
<div style='
display: grid;
grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
gap: 15px;
font-size: 16px;
'>
<div style='
padding: 12px;
border-bottom: 1px solid rgba(255,255,255,0.1);
'>
<strong>{$hours_text}</strong><br>
<span style='font-size: 20px; font-weight: bold;'>" . number_format($hours) . " " . ($language === 'ar' ? 'ساعة' : 'hours') . "</span>
</div>
<div style='
padding: 12px;
border-bottom: 1px solid rgba(255,255,255,0.1);
'>
<strong>{$minutes_text}</strong><br>
<span style='font-size: 20px; font-weight: bold;'>" . number_format($minutes) . " " . ($language === 'ar' ? 'دقيقة' : 'minutes') . "</span>
</div>
<div style='
padding: 12px;
border-bottom: 1px solid rgba(255,255,255,0.1);
'>
<strong>{$days_text}</strong><br>
<span style='font-size: 20px; font-weight: bold;'>" . number_format($total_days, 2) . " " . ($language === 'ar' ? 'يوم' : 'days') . "</span>
</div>
<div style='
padding: 12px;
border-bottom: 1px solid rgba(255,255,255,0.1);
'>
<strong>{$weeks_text}</strong><br>
<span style='font-size: 20px; font-weight: bold;'>" . number_format($total_weeks, 2) . " " . ($language === 'ar' ? 'أسبوع' : 'weeks') . "</span>
</div>
<div style='
padding: 12px;
border-bottom: 1px solid rgba(255,255,255,0.1);
'>
<strong>{$seconds_text}</strong><br>
<span style='font-size: 20px; font-weight: bold;'>" . number_format($total_minutes * 60) . " " . ($language === 'ar' ? 'ثانية' : 'seconds') . "</span>
</div>
</div>
</div>
<!-- Time Comparison Information -->
<div style='
background: rgba(255,255,255,0.1);
padding: 20px;
border-radius: 10px;
margin-top: 20px;
'>
<h4 style='
margin: 0 0 15px 0;
text-align: center;
color: white;
'>
" . ($language === 'ar' ? '⏰ معلومات الوقت' : '⏰ Time Information') . "
</h4>
<div style='
display: grid;
grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
gap: 10px;
font-size: 14px;
'>
<div>
<strong>" . ($language === 'ar' ? 'الوقت المستهدف:' : 'Target Time:') . "</strong><br>
{$full_target_datetime}
</div>
<div>
<strong>" . ($language === 'ar' ? 'اليوم المستهدف:' : 'Target Day:') . "</strong><br>
{$target_day_of_week}" . ($language === 'ar' ? " ({$target_day_ar})" : "") . "
</div>
<div>
<strong>" . ($language === 'ar' ? 'التوقيت الحالي:' : 'Current Time:') . "</strong><br>
{$current_full_datetime}
</div>
<div>
<strong>" . ($language === 'ar' ? 'نظام 24 ساعة:' : '24-Hour Format:') . "</strong><br>
{$target_time_24h}
</div>
</div>
</div>
<!-- Footer -->
<div style='
text-align: center;
margin-top: 25px;
font-size: 14px;
opacity: 0.8;
padding-top: 15px;
border-top: 1px solid rgba(255,255,255,0.2);
'>
<p>✨ " . ($language === 'ar' ? 'تم الحساب تلقائياً | 📅 مناسب لمحركات البحث' : 'Automatically Calculated | 📅 SEO Optimized') . " ✨</p>
</div>
</div>
";
return $output;
} catch (Exception $e) {
return '<div style="
background: #f8d7da;
color: #721c24;
padding: 25px;
border-radius: 10px;
margin: 20px 0;
border: 2px solid #f5c6cb;
text-align: center;
font-size: 16px;
">⚠️ ' . ($language === 'ar' ? 'خطأ في معالجة الوقت: ' : 'Error processing time: ') . $e->getMessage() . '</div>';
}
}
// Ensure the shortcode is registered
add_action('init', function() {
if (!shortcode_exists('time_ago_from_now')) {
add_shortcode('time_ago_from_now', 'time_ago_from_now_handler');
}
});
// Debug function to test if shortcodes are working
add_shortcode('test_time', function() {
return '✅ Time calculator shortcode is working! If you see this, the system is functional.';
});
?>📋 دليل الاستخدام الكامل:
🔹 الاستخدام الأساسي:
[time_ago_from_now hours="1" minutes="30" type="ago" language="en"]
🔹 جميع الأمثلة:
// ما كان الوقت قبل ساعة و 30 دقيقة؟ [time_ago_from_now hours="1" minutes="30" type="ago" language="en"] // ما سيكون الوقت بعد ساعتين و 45 دقيقة؟ [time_ago_from_now hours="2" minutes="45" type="from_now" language="en"] // باللغة العربية - قبل ساعة و 30 دقيقة [time_ago_from_now hours="1" minutes="30" type="ago" language="ar"] // باللغة العربية - بعد 3 ساعات و 15 دقيقة [time_ago_from_now hours="3" minutes="15" type="from_now" language="ar"] // دقائق فقط [time_ago_from_now minutes="45" type="ago" language="en"] // ساعات فقط [time_ago_from_now hours="5" type="from_now" language="en"]
🔹 المعاملات المدعومة:
hours: عدد الساعات (افتراضي: 0)minutes: عدد الدقائق (افتراضي: 0)type:ago(ماضي) أوfrom_now(مستقبل) - الافتراضي:agolanguage:en(إنجليزي) أوar(عربي) - الافتراضي:en
🎯 المميزات الجديدة:
✅ تصميم محسن للسيو:
جمل طبيعية طويلة: "1 hour and 30 minutes ago the time and day was Tuesday, January 21, 2025 at 10:30 AM, while the current time is 12:00 PM on January 21, 2025."
أسئلة ضمنية: "What Time Was It 1 Hour and 30 Minutes Ago?"
تفاصيل شاملة: ساعات، دقائق، أيام، أسابيع، ثواني
معلومات وقت متكاملة: التوقيت الحالي، التوقيت المستهدف، نظام 24 ساعة
✅ واجهة مستخدم متكاملة:
عرض رئيسي: اليوم، التاريخ، الشهر، السنة، الوقت بشكل بارز
شبكة إحصاءات: ساعات، دقائق، دقائق إجمالية
تفاصيل موسعة: جميع الوحدات الزمنية
معلومات وقت: مقارنة بين التوقيت الحالي والمستهدف
✅ دعم متعدد اللغات:
الإنجليزية: نصوص طبيعية ومناسبة للسيو
العربية: ترجمة كاملة مع أيام وأشهر عربية
✅ معالجة أخطاء محسنة:
تحقق من صحة المدخلات (لا توجد قيم سالبة)
تحقق من وجود قيمة واحدة على الأقل
رسائل خطأ واضحة ومفصلة
🧪 طريقة الاختبار:
أولاً: تحقق من عمل الشورت كود
[test_time]
ثانياً: جرب الأمثلة:
[time_ago_from_now hours="1" minutes="30" type="ago" language="en"] [time_ago_from_now hours="2" minutes="15" type="from_now" language="en"] [time_ago_from_now hours="1" minutes="30" type="ago" language="ar"] [time_ago_from_now minutes="45" type="ago" language="en"]
🎨 التصميم المختلف:
لاحظ أني استخدمت تدرج ألوان مختلف (بنفسجي-أزرق) للتمييز عن:
شورت كود الأشهر (برتقالي)
شورت كود الأسابيع (أزرق)
شورت كود أيام الأسبوع (وردي)
الآن لديك شورت كود متكامل لحساب الأوقات مع جميع مميزات الـSEO والتصميم الجميل والسطر الطويل المناسب للسيو!
Comments
Post a Comment