simple csv generator
$users = User::role('customer')->with('profile')->get();
$us = [
['username', 'email', 'nome', 'cognome', 'indirizzo', 'telefono1', 'telefono2']
];
foreach ($users as $user) {
array_push($us, [
$user->name,
$user->email,
$user->profile->first_name,
$user->profile->last_name,
$user->profile->address . ' ' . $user->profile->municipality,
$user->profile->phone,
$user->profile->mobile
]);
}
$fp = fopen(storage_path(). '/users.csv', 'w');
// Loop through file pointer and a line
foreach ($us as $fields) {
fputcsv($fp, $fields);
}
DateTime Interval
$interval = 'P1D';
try {
$x = \Carbon\Carbon::parse($this->times[0])->diffInDays(\Carbon\Carbon::parse($this->times[1]));
if ($x > 90 && $this->date_format == 'm-d-Y') {
$this->date_format = 'u-Y';
$interval = 'P1W';
}
if ($x > 120) {
$this->date_format = 'm-Y';
$interval = 'P1M';
}
} catch (\Throwable $th) { //throw $th; }
$date_format = $this->date_format;
try {
$start_period = \Carbon\Carbon::parse($this->times[0])->startOfDay();
$end_period = \Carbon\Carbon::parse($this->times[1])->endOfDay();
} catch (\Throwable $th) {
$start_period = now()->startOfMonth()->startOfDay();
$end_period = now()->endOfMonth()->endOfDay();
}
$period = new DatePeriod(
new \DateTime($start_period),
new DateInterval($interval),
new \DateTime($end_period)
);