How to convert a math expression into string in php?

490 Views Asked by At

I just wanted to find a way to convert the mathematical expression into a string. There are a lot of solutions for converting the string into a mathematical form or calculate the mathematical expression within a string but not a single solution for my problem.

$math_expression = 10/24/2020;
$result = strval($math_expression);
echo $result;

The output is:

0.00020627062706271

I have also tried

(string)$math_expression

But the output is the same 0.00020627062706271.

I want the output to be '10/24/2020'.

Here is the controller function:

    public function importScores(Request $request)
    {
        $fileName = time() . '.' . $request->file->getClientOriginalExtension();
        $request->file->move(storage_path('upload'), $fileName);
        $filePath = storage_path('upload/' . $fileName);
        Excel::import(new ScoresImport, $filePath);


        $scores = Score::all();
        return response()->json(['scores' => $scores, 'request' => $filePath], 200);
    }


Here is the ScoresImport Class:

<?php

namespace App\Imports;

use App\Models\Score;
use Carbon\Carbon;
use Illuminate\Support\Collection;
use Maatwebsite\Excel\Concerns\ToCollection;

class ScoresImport implements ToCollection
{
    public function collection(Collection $rows)
    {
        foreach ($rows as $key => $row) {
            if ($key > 0) {

                Score::create([
                    'user_id'     => $row[0],
                    'result'    => $row[1],
                    'grade_date'   => Carbon::createFromFormat('m/d/Y', $row[2])->format('m/d/Y'), //Coming from excel file in 11/23/2020 MM/dd/yyy format
                    // 'grade_date'   => $row[2], // I also tried as this and with (string)$row[2] && strval($row[2])
                    'ie'   => $row[3]
                ]);
            }
        }
    }


    public function chunkSize(): int
    {
        return 1000;
    }

    public function batchSize(): int
    {
        return 1000;
    }
}

2

There are 2 best solutions below

0
On BEST ANSWER

Well, I don't think that it has to do with PHP as all math operations will be calculated under any circumstances.

I have not used the package you have mentioned. But by just reviewing their code, I realized it is using another package called "PhpOffice\PhpSpreadsheet" as a dependency. The package has a function named excelToDateTimeObject that I think will help you to solve the challenge.

So if I assume you are using version:3.1 of the Laravel-Excel, your controller would be something like:

<?php

// ...

use PhpOffice\PhpSpreadsheet\Shared\Date;

class ScoresImport implements ToCollection
{
    public function collection(Collection $rows)
    {
        foreach ($rows as $key => $row) {
            if ($key > 0) {
               $phpDateObject = Date::excelToDateTimeObject($row[2]);
               $gradeDateToCreate = Carbon::instance($phpDateObject)->format('m/d/Y');

                Score::create([
                    'user_id'    => $row[0],
                    'result'     => $row[1],
                    'grade_date' => $gradeDateToCreate,
                    'ie'   => $row[3]
                ]);
            }
        }
    }

// ...

}
0
On

I think this is not possible, as long as you dont import the data as a string, since the variable saves the value you import, it already calculates the value.

Maybe you should use the CONCATENATE function in Excel, to add quotation marks before and after your expression.