كم يكون التاريخ بعد او قبل شهور او اسابيع او ايام من (الان) php مع امكانية اضافة تاريخ مخصص v2
نعم، سأضيف خاصية التاريخ المخصص مع الحفاظ على جميع التطويرات السابقة. إليك الكود المحدث:
<?php // إضافة شورت كود للآلة الحاسبة الشاملة add_shortcode('universal_date_calculator', 'universal_date_calculator_handler'); function universal_date_calculator_handler($atts) { // الحصول على المعلمات المدخلة $atts = shortcode_atts(array( 'months' => '2', 'weeks' => '0', 'days' => '0', 'action' => 'ago', 'language' => 'en', 'start_date' => '' // إضافة معلمة التاريخ المخصص ), $atts); $months = intval($atts['months']); $weeks = intval($atts['weeks']); $days = intval($atts['days']); $action = $atts['action']; $language = $atts['language']; $start_date = $atts['start_date']; // تحديد التاريخ الأساسي (المخصص أو الحالي) $now = new DateTime(); $base_date = $now; $base_date_display = $language === 'ar' ? 'الآن (Now)' : 'Now'; if (!empty($start_date)) { try { $base_date = new DateTime($start_date); $base_date_display = $base_date->format('F j, Y'); } catch (Exception $e) { // إذا كان التاريخ غير صالح، نستخدم التاريخ الحالي $base_date = $now; $base_date_display = $language === 'ar' ? 'الآن (Now)' : 'Now'; } } $result_date = clone $base_date; // حساب التاريخ بناءً على الإعدادات if ($action === 'ago') { $result_date->modify("-$months months"); $result_date->modify("-" . ($weeks * 7 + $days) . " days"); } else { $result_date->modify("+$months months"); $result_date->modify("+" . ($weeks * 7 + $days) . " days"); } // حساب الوقت المنقضي $interval = $base_date->diff($result_date); $total_days = $interval->days; // بناء الوصف بناءً على الوحدات المستخدمة $time_description = build_time_description($months, $weeks, $days, $action, $language); $seo_description = build_seo_description($months, $weeks, $days, $action, $result_date, $total_days, $language, $base_date_display); // بناء واجهة النتيجة ob_start(); ?> <div class="universal-date-calculator-container"> <div class="result-box" style="background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%); border-radius: 10px; padding: 40px; margin: 30px 0; text-align: center; box-shadow: 0 4px 15px rgba(0,0,0,0.1); border: 1px solid #e1e1e1;"> <div class="result" style="font-size: 52px; font-weight: 800; color: #e74c3c; margin: 0;"><?php echo $result_date->format('F j, Y'); ?></div> <div class="date-info" style="font-size: 24px; font-weight: 600; color: #2c3e50; margin-top: 15px;"><?php echo $result_date->format('l'); ?></div> </div> <div class="base-date-info" style="background: rgba(255,255,255,0.8); padding: 15px; border-radius: 8px; margin: 15px 0; text-align: center; border: 1px solid #1877c1;"> <div style="font-size: 16px; color: #2c3e50;"> <?php if ($language === 'ar'): ?> 📅 <strong>التاريخ الأساسي:</strong> <?php echo $base_date_display; ?> <?php else: ?> 📅 <strong>Base Date:</strong> <?php echo $base_date_display; ?> <?php endif; ?> </div> </div> <div class="units-description" style="background: rgba(255,255,255,0.9); padding: 20px; border-radius: 10px; margin: 20px 0; text-align: center; border: 2px solid #1877c1;"> <h3 style="color: #2c3e50; margin-bottom: 15px;">📊 Calculation Units Used</h3> <div style="display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 15px; font-size: 16px;"> <?php echo build_units_display($months, $weeks, $days, $language); ?> </div> </div> <div class="seo-description" style="font-size: 18px; margin: 40px 0; color: #2c3e50; line-height: 1.7; padding: 25px; background: white; border-radius: 8px; box-shadow: 0 2px 8px rgba(0,0,0,0.05); border-left: 4px solid #1877c1;"> <?php echo $seo_description; ?> </div> </div> <?php return ob_get_clean(); } // دالة لبناء وصف الوحدات المستخدمة function build_time_description($months, $weeks, $days, $action, $language = 'en') { $parts = []; if ($months > 0) { $parts[] = $months . " month" . ($months > 1 ? 's' : ''); } if ($weeks > 0) { $parts[] = $weeks . " week" . ($weeks > 1 ? 's' : ''); } if ($days > 0) { $parts[] = $days . " day" . ($days > 1 ? 's' : ''); } if (empty($parts)) { return $language === 'ar' ? '0 أيام' : '0 days'; } $time_string = implode(', ', $parts); $action_text = $action === 'ago' ? ($language === 'ar' ? 'منذ' : 'ago') : ($language === 'ar' ? 'من الآن' : 'from now'); return $time_string . ' ' . $action_text; } // دالة لبناء وصف SEO function build_seo_description($months, $weeks, $days, $action, $result_date, $total_days, $language = 'en', $base_date = 'Now') { $time_desc = build_time_description($months, $weeks, $days, $action, $language); if ($language === 'ar') { $base_text = ($base_date === 'الآن (Now)' || $base_date === 'Now') ? 'الآن' : $base_date; return " <p><strong>{$time_desc} من {$base_text} كان {$result_date->format('F j, Y')}, وكان {$result_date->format('l')}.</strong></p> <p>تستخدم حسابات التاريخ لتتبع الأحداث الماضية، وتذكر التواريخ المهمة، وتحليل الفترات الزمنية. سواء كنت تحسب تاريخ بدء مشروع، أو ذكرى سابقة، أو تحتاج إلى معرفة متى بدأت فترة محددة، فإن فهم كيفية طرح الأشهر من التاريخ الحالي هو مهارة قيمة.</p> <p>{$time_desc} من {$base_text} يساوي تقريباً {$total_days} يوماً، وهو حوالي " . floor($total_days / 7) . " أسبوعاً.</p> "; } else { $base_text = ($base_date === 'Now') ? 'now' : $base_date; return " <p><strong>{$time_desc} from {$base_text} was {$result_date->format('F j, Y')}, which was a {$result_date->format('l')}.</strong></p> <p>You use date calculations to track past events, remember important dates, and analyze time intervals. Whether you're calculating a project start date, a past anniversary, or need to know when a specific period began, understanding how to subtract months from the current date is a valuable skill.</p> <p>{$time_desc} from {$base_text} equals approximately {$total_days} days, which is about " . floor($total_days / 7) . " weeks.</p> "; } } // دالة لعرض الوحدات function build_units_display($months, $weeks, $days, $language = 'en') { $units = []; if ($months > 0) { $label = $language === 'ar' ? 'شهور' : 'Months'; $units[] = " <div style=\"background: #e8f4fd; padding: 15px; border-radius: 8px; text-align: center;\"> <div style=\"font-size: 24px; font-weight: bold; color: #1877c1;\">{$months}</div> <div style=\"font-size: 14px; color: #2c3e50;\">{$label}</div> </div> "; } if ($weeks > 0) { $label = $language === 'ar' ? 'أسابيع' : 'Weeks'; $units[] = " <div style=\"background: #e8f8f0; padding: 15px; border-radius: 8px; text-align: center;\"> <div style=\"font-size: 24px; font-weight: bold; color: #27ae60;\">{$weeks}</div> <div style=\"font-size: 14px; color: #2c3e50;\">{$label}</div> </div> "; } if ($days > 0) { $label = $language === 'ar' ? 'أيام' : 'Days'; $units[] = " <div style=\"background: #fef9e7; padding: 15px; border-radius: 8px; text-align: center;\"> <div style=\"font-size: 24px; font-weight: bold; color: #f39c12;\">{$days}</div> <div style=\"font-size: 14px; color: #2c3e50;\">{$label}</div> </div> "; } if (empty($units)) { $message = $language === 'ar' ? 'لم يتم تحديد وحدات زمنية' : 'No time units specified'; return " <div style=\"background: #f8f9fa; padding: 15px; border-radius: 8px; text-align: center; grid-column: 1 / -1;\"> <div style=\"font-size: 16px; color: #6c757d;\">⚠️ {$message}</div> </div> "; } return implode('', $units); } // إضافة شورت كود للآلة الحاسبة التفاعلية add_shortcode('interactive_date_calculator', 'interactive_date_calculator_handler'); function interactive_date_calculator_handler() { // معالجة النموذج إذا تم إرساله $result_html = ''; if (isset($_POST['calculate_date'])) { $months = intval($_POST['months'] ?? 2); $weeks = intval($_POST['weeks'] ?? 0); $days = intval($_POST['days'] ?? 0); $action = $_POST['action'] ?? 'ago'; $language = $_POST['language'] ?? 'en'; $start_date = $_POST['start_date'] ?? ''; // تحديد التاريخ الأساسي $base_date = new DateTime(); $base_date_display = $language === 'ar' ? 'الآن (Now)' : 'Now'; if (!empty($start_date)) { try { $base_date = new DateTime($start_date); $base_date_display = $base_date->format('F j, Y'); } catch (Exception $e) { $base_date = new DateTime(); $base_date_display = $language === 'ar' ? 'الآن (Now)' : 'Now'; } } $result_date = clone $base_date; if ($action === 'ago') { $result_date->modify("-$months months"); $result_date->modify("-" . ($weeks * 7 + $days) . " days"); } else { $result_date->modify("+$months months"); $result_date->modify("+" . ($weeks * 7 + $days) . " days"); } // بناء وصف الوحدات للنتيجة $time_desc = build_time_description($months, $weeks, $days, $action, $language); $result_html = ' <div class="calculator-result show" style="background: linear-gradient(135deg, #f0f8ff 0%, #e6f7ff 100%); border-radius: 10px; padding: 25px; margin-top: 25px; text-align: center; border: 1px solid #c3e6ff; display: block; animation: fadeIn 0.5s ease-in-out;"> <div class="result-title" style="font-size: 20px; font-weight: 600; color: #2c3e50; margin-bottom: 15px;">Calculation Result</div> <div class="calculator-time" style="font-size: 42px; font-weight: 800; color: #e74c3c; margin: 0;">' . $result_date->format('F j, Y') . '</div> <div class="calculator-date" style="font-size: 20px; font-weight: 600; color: #2c3e50; margin-top: 10px;">' . $result_date->format('l') . '</div> <div class="base-date-info" style="background: rgba(255,255,255,0.8); padding: 10px; border-radius: 6px; margin-top: 10px; font-size: 16px; color: #2c3e50;"> <strong>' . ($language === 'ar' ? 'من: ' : 'From: ') . $base_date_display . '</strong> </div> <div class="units-summary" style="background: rgba(255,255,255,0.8); padding: 15px; border-radius: 8px; margin-top: 15px; font-size: 16px; color: #2c3e50;"> <strong>' . $time_desc . '</strong> </div> </div>'; } ob_start(); ?> <style> @keyframes fadeIn { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: translateY(0); } } .start-date-input { width: 200px; padding: 10px; border: 1px solid #ddd; border-radius: 6px; text-align: center; font-size: 16px; } .date-format-hint { font-size: 12px; color: #666; margin-top: 5px; } @media (max-width: 768px) { .calculator-form { flex-direction: column; align-items: center; } .start-date-input { width: 100%; max-width: 200px; } } </style> <div class="calculator-container" style="background: white; border-radius: 10px; padding: 30px; margin: 30px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.1);"> <h2 class="calculator-title" style="font-size: 24px; font-weight: 700; color: #2c3e50; text-align: center; margin-bottom: 20px;">"Add or Subtract Time" Calculator</h2> <form class="calculator-form" method="post" style="display: flex; flex-wrap: wrap; justify-content: center; gap: 15px; margin-bottom: 20px;"> <div class="input-group" style="display: flex; flex-direction: column; align-items: center;"> <span class="input-label" style="font-size: 14px; margin-bottom: 5px; color: #666;">Start Date (Optional)</span> <input type="text" class="start-date-input" name="start_date" value="<?php echo $_POST['start_date'] ?? ''; ?>" placeholder="YYYY-MM-DD or leave empty" style="width: 200px; padding: 10px; border: 1px solid #ddd; border-radius: 6px; text-align: center; font-size: 16px;"> <div class="date-format-hint" style="font-size: 12px; color: #666; margin-top: 5px;">Format: YYYY-MM-DD</div> </div> <div class="input-group" style="display: flex; flex-direction: column; align-items: center;"> <span class="input-label" style="font-size: 14px; margin-bottom: 5px; color: #666;">Months</span> <input type="number" class="time-input" name="months" value="<?php echo $_POST['months'] ?? 2; ?>" min="0" style="width: 100px; padding: 10px; border: 1px solid #ddd; border-radius: 6px; text-align: center; font-size: 16px;"> </div> <div class="input-group" style="display: flex; flex-direction: column; align-items: center;"> <span class="input-label" style="font-size: 14px; margin-bottom: 5px; color: #666;">Weeks</span> <input type="number" class="time-input" name="weeks" value="<?php echo $_POST['weeks'] ?? 0; ?>" min="0" style="width: 100px; padding: 10px; border: 1px solid #ddd; border-radius: 6px; text-align: center; font-size: 16px;"> </div> <div class="input-group" style="display: flex; flex-direction: column; align-items: center;"> <span class="input-label" style="font-size: 14px; margin-bottom: 5px; color: #666;">Days</span> <input type="number" class="time-input" name="days" value="<?php echo $_POST['days'] ?? 0; ?>" min="0" style="width: 100px; padding: 10px; border: 1px solid #ddd; border-radius: 6px; text-align: center; font-size: 16px;"> </div> <div class="input-group" style="display: flex; flex-direction: column; align-items: center;"> <span class="input-label" style="font-size: 14px; margin-bottom: 5px; color: #666;">Action</span> <select class="select-input" name="action" style="width: 120px; padding: 10px; border: 1px solid #ddd; border-radius: 6px; font-size: 16px;"> <option value="from-now" <?php echo (($_POST['action'] ?? 'ago') === 'from-now') ? 'selected' : ''; ?>>From Now</option> <option value="ago" <?php echo (($_POST['action'] ?? 'ago') === 'ago') ? 'selected' : ''; ?>>Ago</option> </select> </div> <div class="input-group" style="display: flex; flex-direction: column; align-items: center;"> <span class="input-label" style="font-size: 14px; margin-bottom: 5px; color: #666;">Language</span> <select class="select-input" name="language" style="width: 120px; padding: 10px; border: 1px solid #ddd; border-radius: 6px; font-size: 16px;"> <option value="en" <?php echo (($_POST['language'] ?? 'en') === 'en') ? 'selected' : ''; ?>>English</option> <option value="ar" <?php echo (($_POST['language'] ?? 'en') === 'ar') ? 'selected' : ''; ?>>العربية</option> </select> </div> <button type="submit" name="calculate_date" class="calculate-button" style="background: #1877c1; color: white; border: none; border-radius: 6px; padding: 12px 24px; font-size: 16px; font-weight: 600; cursor: pointer; transition: background 0.3s; margin-top: 20px;">Calculate</button> </form> <?php echo $result_html; ?> </div> <?php return ob_get_clean(); } ?>
التطويرات الجديدة:
1. ✅ إضافة خاصية التاريخ المخصص
أضفت معلمة
start_dateفي الشورت كودإذا تم تحديد تاريخ مخصص، يتم استخدامه كتاريخ أساسي
إذا لم يتم تحديد تاريخ، يستخدم التاريخ الحالي (الآن)
2. ✅ الحفاظ على جميع التطويرات السابقة
جميع المميزات السابقة محفوظة
اللغة الإنجليزية والعربية
عرض الوحدات المستخدمة
التصميم الأصلي
3. ✅ طريقة الاستخدام:
أمثلة على الشورت كود:
// استخدام التاريخ الحالي (افتراضي) [universal_date_calculator months="2"] // استخدام تاريخ مخصص [universal_date_calculator months="2" start_date="2025-06-24"] // استخدام وحدات متعددة مع تاريخ مخصص [universal_date_calculator months="1" weeks="2" days="3" start_date="2025-06-24" action="from-now"] // باللغة العربية مع تاريخ مخصص [universal_date_calculator months="2" start_date="2025-06-24" language="ar"]
4. ✅ النتائج المتوقعة:
[universal_date_calculator months="2" start_date="2025-06-24"]→ "2 months from June 24, 2025 was..."[universal_date_calculator months="2"]→ "2 months from now was..."[universal_date_calculator days="120" start_date="2025-01-01"]→ "120 days from January 1, 2025 was..."
5. ✅ المميزات الإضافية:
عرض التاريخ الأساسي المستخدم في الحساب
دعم صيغ متعددة للتاريخ (YYYY-MM-DD)
معالجة الأخطاء إذا كان التاريخ غير صالح
دعم كامل في الآلة الحاسبة التفاعلية
الآن يمكنك حساب التواريخ من أي تاريخ مخصص تريده مع الحفاظ على جميع المميزات السابقة!
Comments
Post a Comment