如何将标准日期转换为JDE Oracle适配的Julian日期?求ASP转PHP(Laravel)的正确实现
Correct PHP/Laravel Implementation for JDE Julian Date Conversion
Let's fix your function step by step. The original ASP logic is solid, but your PHP version had several key mistakes (like using Carbon::now() instead of the input date, incorrect timestamp calculations, and wrong year extraction). Here's the working implementation that matches the ASP function's behavior:
use Carbon\Carbon; function data_giuliana($inputDate) { // Parse the input date into a Carbon instance (handles "YYYY-MM-DD" format) $date = Carbon::parse($inputDate); // Get last two digits of the year (matches ASP's substr(2,2)) $anno = $date->format('y'); // Get timestamp of January 1st of the input year at 00:00:00 $yearFirstDay = Carbon::create($date->year, 1, 1)->startOfDay()->timestamp; // Convert timestamp to number of days (divide by 86400 seconds per day, not 86400000 milliseconds) $yearFirstDayDays = floor($yearFirstDay / 86400); // Get timestamp of input date at 00:00:00, convert to days $inputDateDays = floor($date->startOfDay()->timestamp / 86400); // Calculate day of year, pad with leading zeros to 3 digits $giorno = str_pad($inputDateDays - $yearFirstDayDays, 3, '0', STR_PAD_LEFT); // Build the JDE Julian date: "1" + 2-digit year + 3-digit day of year return "1{$anno}{$giorno}"; }
What Was Wrong With Your Original PHP Code?
- You relied on
Carbon::now()everywhere instead of using the passed$testdate—so it always used today's date instead of the target date $annowas set to a fullY-m-ddate string instead of just the last two digits of the input year- You used
86400000(milliseconds) but PHP'stimestampreturns seconds, so we need to divide by86400(seconds per day) - The line
ceil(Carbon::now()->toDateTimeString() / 86400000)is invalid—you can't divide a date string by a number; we need to use the timestamp value instead
Test Example
For input 2022-08-09:
- Year last two digits:
22 - Day of year: August 9 is the 221st day of 2022
- Result:
122221(matches the expected JDE Julian format)
Additional Notes
- This function accepts any valid date string that Carbon can parse (not just
YYYY-MM-DD) - Using
startOfDay()ensures time components don't interfere with the day-count calculation str_pad()is a cleaner PHP alternative to the ASP string padding logic
内容的提问来源于stack exchange,提问作者user18182496
相关产品推荐
相关产品推荐

