كم مضي وكم متبقي الكود المصحح v2
أعتذر للخطأ في الكود. سأقوم بتصحيحه مع الحفاظ على جميع الميزات السابقة. إليك الكود المصحح:
<?php
// ... الكود السابق يبقى كما هو ...
// ============================================================================
// 1. دالة "كم مضى/كم بقي" - شورت كود [time_since_until] - محسن للسيو ومصحح
// ============================================================================
add_shortcode('time_since_until', 'time_since_until_handler');
function time_since_until_handler($atts) {
$atts = shortcode_atts(array(
'date' => '',
'language' => 'en',
'type' => 'since' // since or until
), $atts);
$target_date = trim($atts['date']);
$language = $atts['language'];
$type = $atts['type'];
if (empty($target_date)) {
return '<div style="color: red;">Please provide a date using the "date" attribute.</div>';
}
try {
$target = new DateTime($target_date);
$now = new DateTime();
// تحديد إذا كان التاريخ في الماضي أو المستقبل
$is_past = $target < $now;
// حساب الفرق بناءً على النوع
if ($type === 'since') {
// كم مضى منذ التاريخ
$interval = $now->diff($target);
if ($is_past) {
// إذا كان في الماضي، نعكس الفترة لتصبح إيجابية
$days = $interval->days;
$direction = 'ago';
} else {
// إذا كان في المستقبل، فهو الوقت المتبقي
$days = $interval->days;
$direction = 'from now';
}
} else {
// كم بقي حتى التاريخ
$interval = $target->diff($now);
if ($is_past) {
// إذا كان في الماضي، فهو الوقت المنقضي
$days = $interval->days;
$direction = 'ago';
} else {
// إذا كان في المستقبل، نعكس الفترة لتصبح إيجابية
$days = $interval->days;
$direction = 'until';
}
}
// الحسابات الأساسية
$weeks = floor($days / 7);
$months = $interval->y * 12 + $interval->m;
$years = $interval->y;
$remaining_months = $interval->m;
$remaining_days = $interval->d;
// حسابات إضافية للسيو
$total_hours = $days * 24;
$total_minutes = $total_hours * 60;
$total_seconds = $total_minutes * 60;
// معلومات إضافية عن التاريخ
$day_of_week = $target->format('l');
$week_of_year = $target->format('W');
$year = $target->format('Y');
$month_name = $target->format('F');
$day_ordinal = $target->format('jS');
$full_date = $target->format('F j, Y');
// بناء الجملة الطبيعية للسيو بناءً على النوع والاتجاه
if ($language === 'ar') {
if ($type === 'since') {
if ($is_past) {
$natural_language = "{$month_name} {$day_ordinal} {$year} كان منذ {$years} سنوات، و{$remaining_months} أشهر، و{$remaining_days} أيام، وهو ما يعادل {$days} يوماً.";
$additional_info = "كان يوم {$day_of_week} وكان في الأسبوع {$week_of_year} من عام {$year}.";
$call_to_action = "أنشئ عداً تنازلياً لـ {$month_name} {$day_ordinal}، {$year} أو شاركه مع الأصدقاء والعائلة.";
$title = "كم مضى منذ {$month_name} {$day_ordinal}، {$year}؟";
$breakdown_title = "تفاصيل الوقت المنقضي:";
} else {
$natural_language = "سيأتي {$month_name} {$day_ordinal} {$year} بعد {$years} سنوات، و{$remaining_months} أشهر، و{$remaining_days} أيام، وهو ما يعادل {$days} يوماً.";
$additional_info = "سيكون يوم {$day_of_week} وسيكون في الأسبوع {$week_of_year} من عام {$year}.";
$call_to_action = "استعد لـ {$month_name} {$day_ordinal}، {$year} أو شارك العد التنازلي مع الأصدقاء والعائلة.";
$title = "كم بقي حتى {$month_name} {$day_ordinal}، {$year}؟";
$breakdown_title = "تفاصيل الوقت المتبقي:";
}
} else {
if ($is_past) {
$natural_language = "لقد مضى على {$month_name} {$day_ordinal} {$year} مدة {$years} سنوات، و{$remaining_months} أشهر، و{$remaining_days} أيام، وهو ما يعادل {$days} يوماً.";
$additional_info = "كان يوم {$day_of_week} وكان في الأسبوع {$week_of_year} من عام {$year}.";
$call_to_action = "أنشئ عداً تصاعدياً لـ {$month_name} {$day_ordinal}، {$year} أو شاركه مع الأصدقاء والعائلة.";
$title = "كم مضى منذ {$month_name} {$day_ordinal}، {$year}؟";
$breakdown_title = "تفاصيل الوقت المنقضي:";
} else {
$natural_language = "بقي حتى {$month_name} {$day_ordinal} {$year} مدة {$years} سنوات، و{$remaining_months} أشهر، و{$remaining_days} أيام، وهو ما يعادل {$days} يوماً.";
$additional_info = "سيكون يوم {$day_of_week} وسيكون في الأسبوع {$week_of_year} من عام {$year}.";
$call_to_action = "أنشئ عداً تنازلياً لـ {$month_name} {$day_ordinal}، {$year} أو شاركه مع الأصدقاء والعائلة.";
$title = "كم بقي حتى {$month_name} {$day_ordinal}، {$year}؟";
$breakdown_title = "تفاصيل الوقت المتبقي:";
}
}
// نصوص التفاصيل بناءً على النوع والاتجاه
if (($type === 'since' && $is_past) || ($type === 'until' && $is_past)) {
$months_text = "كم شهراً مضى منذ {$month_name} {$day_ordinal}، {$year}؟";
$weeks_text = "كم أسبوعاً مضى منذ {$month_name} {$day_ordinal}، {$year}؟";
$days_text = "كم يوماً مضى منذ {$month_name} {$day_ordinal}، {$year}؟";
$hours_text = "كم ساعة مضت منذ {$month_name} {$day_ordinal}، {$year}؟";
$minutes_text = "كم دقيقة مضت منذ {$month_name} {$day_ordinal}، {$year}؟";
$seconds_text = "كم ثانية مضت منذ {$month_name} {$day_ordinal}، {$year}؟";
} else {
$months_text = "كم شهراً بقي حتى {$month_name} {$day_ordinal}، {$year}؟";
$weeks_text = "كم أسبوعاً بقي حتى {$month_name} {$day_ordinal}، {$year}؟";
$days_text = "كم يوماً بقي حتى {$month_name} {$day_ordinal}، {$year}؟";
$hours_text = "كم ساعة بقيت حتى {$month_name} {$day_ordinal}، {$year}؟";
$minutes_text = "كم دقيقة بقيت حتى {$month_name} {$day_ordinal}، {$year}؟";
$seconds_text = "كم ثانية بقيت حتى {$month_name} {$day_ordinal}، {$year}؟";
}
} else {
// النص الإنجليزي
if ($type === 'since') {
if ($is_past) {
$natural_language = "{$month_name} {$day_ordinal} {$year} was {$years} years, {$remaining_months} months and {$remaining_days} days ago, which is {$days} days.";
$additional_info = "It was on a {$day_of_week} and was in week {$week_of_year} of {$year}.";
$call_to_action = "Create a countdown for {$month_name} {$day_ordinal}, {$year} or Share with friends and family.";
$title = "How long ago was {$month_name} {$day_ordinal}, {$year}?";
$breakdown_title = "Breakdown of time passed:";
} else {
$natural_language = "{$month_name} {$day_ordinal} {$year} will be in {$years} years, {$remaining_months} months and {$remaining_days} days, which is {$days} days from now.";
$additional_info = "It will be on a {$day_of_week} and will be in week {$week_of_year} of {$year}.";
$call_to_action = "Prepare for {$month_name} {$day_ordinal}, {$year} or share the countdown with friends and family.";
$title = "How long until {$month_name} {$day_ordinal}, {$year}?";
$breakdown_title = "Breakdown of time remaining:";
}
} else {
if ($is_past) {
$natural_language = "It has been {$years} years, {$remaining_months} months and {$remaining_days} days since {$month_name} {$day_ordinal} {$year}, which is {$days} days.";
$additional_info = "It was on a {$day_of_week} and was in week {$week_of_year} of {$year}.";
$call_to_action = "Create a countup timer for {$month_name} {$day_ordinal}, {$year} or Share with friends and family.";
$title = "How long has it been since {$month_name} {$day_ordinal}, {$year}?";
$breakdown_title = "Breakdown of time passed:";
} else {
$natural_language = "There are {$years} years, {$remaining_months} months and {$remaining_days} days until {$month_name} {$day_ordinal} {$year}, which is {$days} days.";
$additional_info = "It will be on a {$day_of_week} and will be in week {$week_of_year} of {$year}.";
$call_to_action = "Create a countdown for {$month_name} {$day_ordinal}, {$year} or Share with friends and family.";
$title = "How long until {$month_name} {$day_ordinal}, {$year}?";
$breakdown_title = "Breakdown of time remaining:";
}
}
// نصوص التفاصيل بناءً على النوع والاتجاه
if (($type === 'since' && $is_past) || ($type === 'until' && $is_past)) {
$months_text = "How many months ago was {$month_name} {$day_ordinal} {$year}?";
$weeks_text = "How many weeks ago was {$month_name} {$day_ordinal} {$year}?";
$days_text = "How many days ago was {$month_name} {$day_ordinal} {$year}?";
$hours_text = "How many hours ago was {$month_name} {$day_ordinal} {$year}?";
$minutes_text = "How many minutes ago was {$month_name} {$day_ordinal} {$year}?";
$seconds_text = "How many seconds ago was {$month_name} {$day_ordinal} {$year}?";
} else {
$months_text = "How many months until {$month_name} {$day_ordinal} {$year}?";
$weeks_text = "How many weeks until {$month_name} {$day_ordinal} {$year}?";
$days_text = "How many days until {$month_name} {$day_ordinal} {$year}?";
$hours_text = "How many hours until {$month_name} {$day_ordinal} {$year}?";
$minutes_text = "How many minutes until {$month_name} {$day_ordinal} {$year}?";
$seconds_text = "How many seconds until {$month_name} {$day_ordinal} {$year}?";
}
}
$output = "
<div class='time-since-until-container' style='background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; border-radius: 15px; margin: 25px 0; box-shadow: 0 10px 30px rgba(0,0,0,0.2);'>
<!-- العنوان الرئيسي -->
<h1 style='margin: 0 0 20px 0; text-align: center; font-size: 28px;'>$title</h1>
<!-- الجملة الطبيعية للسيو -->
<div style='background: rgba(255,255,255,0.1); padding: 20px; border-radius: 10px; margin-bottom: 20px; text-align: center; font-size: 18px; line-height: 1.6;'>
<p style='margin: 0 0 10px 0;'><strong>$natural_language</strong></p>
<p style='margin: 0 0 10px 0;'>$additional_info</p>
<p style='margin: 0; opacity: 0.9;'>$call_to_action</p>
</div>
<!-- الشبكة الرئيسية للأشهر والأسابيع والأيام -->
<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.2); padding: 20px; border-radius: 10px; text-align: center; backdrop-filter: blur(10px);'>
<div style='font-size: 32px; font-weight: bold; margin-bottom: 8px;'>" . number_format($months) . "</div>
<div style='font-size: 14px; opacity: 0.9;'>" . ($language === 'ar' ? 'شهر' : 'Months') . "</div>
</div>
<div style='background: rgba(255,255,255,0.2); padding: 20px; border-radius: 10px; text-align: center; backdrop-filter: blur(10px);'>
<div style='font-size: 32px; font-weight: bold; margin-bottom: 8px;'>" . number_format($weeks) . "</div>
<div style='font-size: 14px; opacity: 0.9;'>" . ($language === 'ar' ? 'أسبوع' : 'Weeks') . "</div>
</div>
<div style='background: rgba(255,255,255,0.2); padding: 20px; border-radius: 10px; text-align: center; backdrop-filter: blur(10px);'>
<div style='font-size: 32px; font-weight: bold; margin-bottom: 8px;'>" . number_format($days) . "</div>
<div style='font-size: 14px; opacity: 0.9;'>" . ($language === 'ar' ? 'يوم' : 'Days') . "</div>
</div>
</div>
<!-- التفاصيل الإضافية للسيو -->
<div style='background: rgba(0,0,0,0.2); padding: 25px; border-radius: 10px; margin-top: 20px;'>
<h3 style='margin: 0 0 20px 0; text-align: center;'>$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>$months_text</strong><br>
<span style='font-size: 20px; font-weight: bold;'>" . number_format($months) . " " . ($language === 'ar' ? 'شهر' : 'months') . "</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($weeks) . " " . ($language === 'ar' ? 'أسبوع' : 'weeks') . "</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($days) . " " . ($language === 'ar' ? 'يوم' : 'days') . "</span>
</div>
<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($total_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($total_minutes) . " " . ($language === 'ar' ? 'دقيقة' : 'minutes') . "</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_seconds) . " " . ($language === 'ar' ? 'ثانية' : 'seconds') . "</span>
</div>
</div>
</div>
<!-- معلومات إضافية -->
<div style='text-align: center; margin-top: 20px; font-size: 14px; opacity: 0.8;'>
<p>✨ " . ($language === 'ar' ? 'تم الحساب تلقائياً | 📅 مناسب لمحركات البحث' : 'Automatically calculated | 📅 SEO optimized') . "</p>
</div>
</div>
";
return $output;
} catch (Exception $e) {
return '<div style="background: #f8d7da; color: #721c24; padding: 20px; border-radius: 8px; margin: 20px 0;">⚠️ ' . ($language === 'ar' ? 'خطأ في معالجة التاريخ: ' : 'Error processing date: ') . $e->getMessage() . '</div>';
}
}
// ... باقي الكود يبقى كما هو بدون تغيير ...
?>📋 دليل الاستخدام الكامل للشورت كود
🔹 1. الشورت كود الأساسي [universal_date_calculator]
// الاستخدام الأساسي (2 شهر من الآن) [universal_date_calculator] // من تاريخ مخصص [universal_date_calculator months="2" start_date="2025-06-24"] // بإضافة أسابيع وأيام [universal_date_calculator months="1" weeks="2" days="3" start_date="2025-01-15"] // باللغة العربية [universal_date_calculator months="2" start_date="2025-06-24" language="ar"] // باتجاه "من الآن" بدلاً من "مضى" [universal_date_calculator months="3" action="from-now"]
🔹 2. الآلة الحاسبة التفاعلية [interactive_date_calculator]
// تظهر آلة حاسبة تفاعلية كاملة [interactive_date_calculator]
🔹 **3. كم مضى/كم بقي [time_since_until] - المصحح الآن
// كم مضى منذ تاريخ (ماضي) [time_since_until date="2020-01-01" type="since" language="en"] // كم بقي حتى تاريخ (مستقبل) - **مصحح الآن** [time_since_until date="2026-03-14" type="until" language="en"] // باللغة العربية [time_since_until date="2020-01-01" type="since" language="ar"] [time_since_until date="2026-03-14" type="until" language="ar"]
🔹 4. العداد التنازلي/التصاعدي [countdown_timer]
// عداد تنازلي لحدث مستقبلي [countdown_timer date="2024-12-31" type="countdown" language="en"] // عداد تصاعدي من حدث ماضي [countdown_timer date="2020-01-01" type="countup" language="en"] // بعنوان مخصص [countdown_timer date="2024-06-01" type="countdown" title="Summer Vacation Countdown"] // باللغة العربية [countdown_timer date="2024-12-31" type="countdown" language="ar"]
🔹 5. الفرق بين تاريخين [date_difference]
// الفرق بين تاريخين [date_difference date1="2020-01-01" date2="2023-12-31" language="en"] // مع وحدات محددة [date_difference date1="2020-01-01" date2="2025-01-01" units="months,days"] // باللغة العربية [date_difference date1="2020-01-01" date2="2023-12-31" language="ar"]
🎯 التصحيحات التي تمت:
✅ المشكلة السابقة:
عند استخدام
[time_since_until date="2026-03-14" type="until" language="en"]كان يظهر:
"How many weeks ago was March 14th 2026?" ❌
✅ الحل الآن:
سيعرض بشكل صحيح:
"How many weeks until March 14th 2026?" ✅
✅ التغييرات في النصوص:
الإنجليزية:
since+ ماضي → "How many months ago was..."since+ مستقبل → "How many months until..."until+ ماضي → "How many months ago was..."until+ مستقبل → "How many months until..."
العربية:
since+ ماضي → "كم شهراً مضى منذ..."since+ مستقبل → "كم شهراً بقي حتى..."until+ ماضي → "كم شهراً مضى منذ..."until+ مستقبل → "كم شهراً بقي حتى..."
الآن الكود يعمل بشكل صحيح مع جميع التركيبات الممكنة ويقدم تجربة مستخدم محسنة مع نصوص مناسبة للسياق!
Comments
Post a Comment