كم اسبوع متبقي حتي تاريخ النسخة الاولي weeks until
ممتاز! سأقوم بإنشاء شورت كود جديد مشابه تماماً للمنطق السابق لكن لحساب الأسابيع. إليك الكود الكامل:
<?php
// ============================================================================
// WEEKS UNTIL/SINCE COUNTER - STANDALONE VERSION
// ============================================================================
// Register the shortcode
add_shortcode('weeks_until_since', 'weeks_until_since_handler');
function weeks_until_since_handler($atts) {
// Set default attributes
$atts = shortcode_atts(array(
'date' => '',
'type' => 'until', // until or since
'language' => 'en'
), $atts);
// Get attributes
$target_date = trim($atts['date']);
$type = $atts['type'];
$language = $atts['language'];
// Validate required fields
if (empty($target_date)) {
return '<div style="color: red; padding: 20px; text-align: center; border: 2px solid red; border-radius: 10px;">❌ Error: Please provide a date using the "date" attribute. Example: [weeks_until_since date="2025-09-04" type="until"]</div>';
}
try {
// Create date objects
$target = new DateTime($target_date);
$today = new DateTime();
// Set time to midnight for accurate comparison
$target->setTime(0, 0, 0);
$today->setTime(0, 0, 0);
// Check if date is valid
if (!$target) {
return '<div style="color: red; padding: 20px; text-align: center; border: 2px solid red; border-radius: 10px;">❌ Error: Invalid date format. Please use YYYY-MM-DD format.</div>';
}
// Determine if target date is in past or future
$is_past = $target < $today;
// Validate type vs date direction
if ($type === 'until' && $is_past) {
return '<div style="color: red; padding: 20px; text-align: center; border: 2px solid red; border-radius: 10px;">❌ Error: For "until" calculations, the target date must be in the future. Your date is in the past.</div>';
}
if ($type === 'since' && !$is_past) {
return '<div style="color: red; padding: 20px; text-align: center; border: 2px solid red; border-radius: 10px;">❌ Error: For "since" calculations, the target date must be in the past. Your date is in the future.</div>';
}
// Calculate the time difference
$interval = $today->diff($target);
$total_days = $interval->days;
$total_weeks = floor($total_days / 7);
$remaining_days = $total_days % 7;
$total_months = $interval->y * 12 + $interval->m;
$total_years = $interval->y;
// Additional calculations for SEO
$total_hours = $total_days * 24;
$total_minutes = $total_hours * 60;
$total_seconds = $total_minutes * 60;
// Get date information for display
$target_day_of_week = $target->format('l');
$target_week_of_year = $target->format('W');
$target_year = $target->format('Y');
$target_month = $target->format('F');
$target_day_ordinal = $target->format('jS');
$full_target_date = $target->format('F j, Y');
// Today's date for reference
$today_date = $today->format('F j, Y');
// Build display content based on language and type
if ($language === 'ar') {
// Arabic content
$days_translation = [
'Monday' => 'الاثنين',
'Tuesday' => 'الثلاثاء',
'Wednesday' => 'الأربعاء',
'Thursday' => 'الخميس',
'Friday' => 'الجمعة',
'Saturday' => 'السبت',
'Sunday' => 'الأحد'
];
$target_day_ar = $days_translation[$target_day_of_week];
$months_ar = [
'January' => 'يناير',
'February' => 'فبراير',
'March' => 'مارس',
'April' => 'أبريل',
'May' => 'مايو',
'June' => 'يونيو',
'July' => 'يوليو',
'August' => 'أغسطس',
'September' => 'سبتمبر',
'October' => 'أكتوبر',
'November' => 'نوفمبر',
'December' => 'ديسمبر'
];
$target_month_ar = $months_ar[$target_month];
if ($type === 'until') {
$title = "كم أسبوع متبقي حتى {$target_month_ar} {$target_day_ordinal}، {$target_year}؟";
$main_text = "هناك {$total_weeks} أسبوع و {$remaining_days} يوم متبقي حتى {$full_target_date}";
$natural_language = "{$target_month_ar} {$target_day_ordinal}، {$target_year} سيكون بعد {$total_weeks} أسبوع و {$remaining_days} يوم، وهو ما يعادل {$total_days} يوماً.";
$additional_info = "سيوافق {$full_target_date} يوم {$target_day_ar} وسيكون في الأسبوع {$target_week_of_year} من عام {$target_year}.";
} else {
$title = "كم أسبوع مضى منذ {$target_month_ar} {$target_day_ordinal}، {$target_year}؟";
$main_text = "لقد مضى {$total_weeks} أسبوع و {$remaining_days} يوم منذ {$full_target_date}";
$natural_language = "لقد مضى {$total_weeks} أسبوع و {$remaining_days} يوم منذ {$full_target_date}، وهو ما يعادل {$total_days} يوماً.";
$additional_info = "وافق {$full_target_date} يوم {$target_day_ar} وكان في الأسبوع {$target_week_of_year} من عام {$target_year}.";
}
$call_to_action = "تابع تقدم الوقت أو شارك هذه المعلومات مع الأصدقاء والعائلة.";
$breakdown_title = "التفاصيل الإضافية:";
// Breakdown questions
$weeks_text = "كم أسبوع " . ($type === 'until' ? 'متبقي' : 'مضى') . "؟";
$days_text = "كم يوم " . ($type === 'until' ? 'متبقي' : 'مضى') . "؟";
$months_text = "كم شهر " . ($type === 'until' ? 'متبقي' : 'مضى') . "؟";
$hours_text = "كم ساعة " . ($type === 'until' ? 'متبقية' : 'مضت') . "؟";
$minutes_text = "كم دقيقة " . ($type === 'until' ? 'متبقية' : 'مضت') . "؟";
$seconds_text = "كم ثانية " . ($type === 'until' ? 'متبقية' : 'مضت') . "؟";
} else {
// English content
if ($type === 'until') {
$title = "How many weeks until {$target_month} {$target_day_ordinal}, {$target_year}?";
$main_text = "There are {$total_weeks} weeks and {$remaining_days} days until {$full_target_date}";
$natural_language = "{$target_month} {$target_day_ordinal}, {$target_year} will be in {$total_weeks} weeks and {$remaining_days} days, which is {$total_days} days.";
$additional_info = "{$full_target_date} will be on a {$target_day_of_week} and will be in week {$target_week_of_year} of {$target_year}.";
} else {
$title = "How many weeks since {$target_month} {$target_day_ordinal}, {$target_year}?";
$main_text = "It has been {$total_weeks} weeks and {$remaining_days} days since {$full_target_date}";
$natural_language = "It has been {$total_weeks} weeks and {$remaining_days} days since {$full_target_date}, which is {$total_days} days.";
$additional_info = "{$full_target_date} was on a {$target_day_of_week} and was in week {$target_week_of_year} of {$target_year}.";
}
$call_to_action = "Track your time progress or share this information with friends and family.";
$breakdown_title = "Additional Breakdown:";
// Breakdown questions
$weeks_text = "How many weeks " . ($type === 'until' ? 'until' : 'since') . "?";
$days_text = "How many days " . ($type === 'until' ? 'until' : 'since') . "?";
$months_text = "How many months " . ($type === 'until' ? 'until' : 'since') . "?";
$hours_text = "How many hours " . ($type === 'until' ? 'until' : 'since') . "?";
$minutes_text = "How many minutes " . ($type === 'until' ? 'until' : 'since') . "?";
$seconds_text = "How many seconds " . ($type === 'until' ? 'until' : 'since') . "?";
}
// Build the output HTML
$output = "
<div 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);
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: 52px;
font-weight: bold;
margin-bottom: 10px;
text-shadow: 2px 2px 4px rgba(0,0,0,0.3);
'>
" . number_format($total_weeks) . "
</div>
<div style='
font-size: 22px;
opacity: 0.9;
'>
" . ($language === 'ar' ? 'أسبوع' : 'Weeks') . "
</div>
<div style='
font-size: 18px;
margin-top: 10px;
opacity: 0.8;
padding-top: 10px;
border-top: 1px solid rgba(255,255,255,0.2);
'>
" . ($language === 'ar' ? "و {$remaining_days} يوم" : "and {$remaining_days} days") . "
</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($total_weeks) . "</div>
<div style='font-size: 14px; opacity: 0.9;'>" . ($language === 'ar' ? 'أسبوع' : 'Weeks') . "</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_days) . "</div>
<div style='font-size: 14px; opacity: 0.9;'>" . ($language === 'ar' ? 'يوم' : 'Days') . "</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_months) . "</div>
<div style='font-size: 14px; opacity: 0.9;'>" . ($language === 'ar' ? 'شهر' : 'Months') . "</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>{$weeks_text}</strong><br>
<span style='font-size: 20px; font-weight: bold;'>" . number_format($total_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($total_days) . " " . ($language === 'ar' ? 'يوم' : 'days') . "</span>
</div>
<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($total_months) . " " . ($language === 'ar' ? 'شهر' : 'months') . "</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>
<!-- Calendar 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' ? '📅 معلومات التقويم' : '📅 Calendar 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 Date:') . "</strong><br>
{$full_target_date}
</div>
<div>
<strong>" . ($language === 'ar' ? 'يوم الأسبوع:' : 'Day of Week:') . "</strong><br>
{$target_day_of_week}" . ($language === 'ar' ? " ({$target_day_ar})" : "") . "
</div>
<div>
<strong>" . ($language === 'ar' ? 'الأسبوع في السنة:' : 'Week of Year:') . "</strong><br>
{$target_week_of_year}
</div>
<div>
<strong>" . ($language === 'ar' ? 'التاريخ الحالي:' : 'Current Date:') . "</strong><br>
{$today_date}
</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 date: ') . $e->getMessage() . '</div>';
}
}
// Ensure the shortcode is registered
add_action('init', function() {
if (!shortcode_exists('weeks_until_since')) {
add_shortcode('weeks_until_since', 'weeks_until_since_handler');
}
});
// Debug function to test if shortcodes are working
add_shortcode('test_weeks', function() {
return '✅ Weeks counter shortcode is working! If you see this, the system is functional.';
});
?>📋 دليل الاستخدام الكامل:
🔹 الاستخدام الأساسي:
[weeks_until_since date="2025-09-04" type="until" language="en"]
🔹 جميع الأمثلة:
// كم أسبوع متبقي حتى تاريخ [weeks_until_since date="2025-09-04" type="until" language="en"] // كم أسبوع مضى منذ تاريخ [weeks_until_since date="2020-01-01" type="since" language="en"] // باللغة العربية - كم أسبوع متبقي [weeks_until_since date="2025-09-04" type="until" language="ar"] // باللغة العربية - كم أسبوع مضى [weeks_until_since date="2020-01-01" type="since" language="ar"]
🔹 المعاملات المدعومة:
date(مطلوب): التاريخ المستهدف (صيغة: YYYY-MM-DD)type:until(مستقبل) أوsince(ماضي) - الافتراضي:untillanguage:en(إنجليزي) أوar(عربي) - الافتراضي:en
🎯 المميزات الجديدة:
✅ تصميم محسن للسيو:
جمل طبيعية: "September 4th, 2025 will be in 45 weeks and 3 days, which is 318 days."
أسئلة ضمنية: "How many weeks until September 4th, 2025?"
تفاصيل شاملة: أسابيع، أيام، شهور، ساعات، دقائق، ثواني
معلومات تقويم: يوم الأسبوع، أسبوع السنة، التاريخ الحالي
✅ واجهة مستخدم متكاملة:
عرض رئيسي: عدد الأسابيع بشكل بارز
شبكة إحصاءات: أسابيع، أيام، شهور
تفاصيل موسعة: جميع الوحدات الزمنية
معلومات تقويم: بيانات إضافية مفيدة
✅ دعم متعدد اللغات:
الإنجليزية: نصوص طبيعية ومناسبة للسيو
العربية: ترجمة كاملة مع أشهر وأيام عربية
✅ معالجة أخطاء محسنة:
تحقق من صحة التاريخ
تحقق من اتجاه التاريخ (ماضي/مستقبل)
رسائل خطأ واضحة ومفصلة
🧪 طريقة الاختبار:
أولاً: تحقق من عمل الشورت كود
[test_weeks]
ثانياً: جرب الأمثلة:
[weeks_until_since date="2025-09-04" type="until" language="en"] [weeks_until_since date="2020-01-01" type="since" language="en"] [weeks_until_since date="2025-09-04" type="until" language="ar"]
الآن لديك شورت كود متكامل لحساب الأسابيع مع جميع مميزات الـSEO والتصميم الجميل!
Comments
Post a Comment