You can simply do something like this
$date = \Carbon\Carbon::now();
$wishDate = \Carbon\Carbon::create($date->year, 6, 15);
if ($date->greatherThan($wishDate) {
// Is after June 15th
} else {
// Is before or on June 15th
}
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I am trying to display one input if the date is less than June 15 and a different input if its June 15 or greater. I know its an if statement but I am not sure how to do this. Can someone please help a newbie out?
You can simply do something like this
$date = \Carbon\Carbon::now();
$wishDate = \Carbon\Carbon::create($date->year, 6, 15);
if ($date->greatherThan($wishDate) {
// Is after June 15th
} else {
// Is before or on June 15th
}
Would that go in the blade template or controller?
use Carbon\Carbon;
$date = Carbon::parse('2018-06-15');
if (Carbon::now()->lt($date)) {
// now is less than $date
} else {
// now is greater than or equal to $date
}
I am a bit confused. I want if its June 15 or before, show a radio button for a dollar amount. If its June 16 or later, show a radio button for a different dollar amount. If I put this in the controller, how do I get it to the form before it is submitted?
I want if its June 15 or before, show a radio button for a dollar amount. If its June 16 or later
That's a little different than what you previously said, but I adjusted it using lte (less than or equal to) I would just return the date
$date = Carbon::parse('2018-06-15');
return view('the-view', compact('date'));
to your view. Then in the view
@if (\Carbon\Carbon::now()->lte($date))
<input type="radio" name="price" value="20">
@else
<input type="radio" name="price" value="40">
@endif
in Laravel do
use Carbon\Carbon;
now()->gte(Carbon::parse('2018-06-15'))
? // greater than or equal
: // else
;
now() is Laravel 5.6 helper for Carbon\Carbon:now()
I did that and I get Undefined variable: date
I did that and I get Undefined variable: date
show your code
public function create()
{
$date = Carbon::parse('2018-06-15');
return view('/#signup', compact($date));
}
@if (\Carbon\Carbon::now()->lte($date))
<input type="radio" name="price" value="20">
@else
<input type="radio" name="price" value="40">
@endif
Sorry, should be return view('the-view', compact('date')); my compact() was wrong.
@artisticre not directly related to your question but do not forget to validate the form input you receive in your controller.
Sending actual prices in form is a bad idea if you do not have proper validation in place. Imagine if I change value to '0'. Am I getting the product for free?
Still get the same error.
$date = Carbon::parse('2018-06-15');
return view('/#signup', compact('date'));
is /#signup an actual view file? I don't see how.
also try php artisan view:clear
I have a one page layout. /#signup is the anchor tag
Ok, but the first parameter to view(), is the view file to load, not a url or hash.
show your whole controller method that is generating this page.
or just put the date in whatever view you're currently using. I have a feeling you're using vue or something for a SPA
@if (\Carbon\Carbon::now()->lte(Carbon::parse('2018-06-15')))
<input type="radio" name="price" value="20">
@else
<input type="radio" name="price" value="40">
@endif
It would be best if $date were dynamic, but you'd have to figure out how to do that with whatever tool(s) you are using since it doesn't sound like you are using a straight laravel/view setup. But that should answer your original question on how to do it.
I imagine the HomeController creates the home page
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
return view('home');
}
}
The SignupController generates the signup form. /#signup is a link from my navbar to that section on the one page layout.
If 'home' is the view containing the form that you're wanting to do this on, then that's the place to do it...
public function index()
{
$date = Carbon::parse('2018-06-15');
return view('home', compact('date'));
}
I am getting the same error
My home controller now
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class HomeController extends Controller
{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Show the application dashboard.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$date = Carbon::parse('2018-06-15');
return view('home', compact('date'));
}
}
On my view
@if (\Carbon\Carbon::now()->lte($date))
<input type="radio" name="price" value="20">
@else
<input type="radio" name="price" value="40">
@endif
So this
@if (\Carbon\Carbon::now()->lte($date))
<input type="radio" name="price" value="20">
@else
<input type="radio" name="price" value="40">
@endif
is in the /resources/views/home.blade.php file?
no its in /resources/views/sections/signup-section.blade.php
My home.blade.php
@extends('layouts.app')
@section('content')
@endsection
/resources/views/layouts/app.blade.php
<!DOCTYPE html>
<html lang="{{ app()->getLocale() }}">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- CSRF Token -->
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>{{ config('app.name', '775 CES Mud Run') }}</title>
<!-- Styles -->
<link href="{{ asset('css/app.css') }}" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$(window).scroll(function(){
if ($(this).scrollTop() > 20) {
$('#scroll').fadeIn();
} else {
$('#scroll').fadeOut();
}
});
$('#scroll').click(function(){
$("html, body").animate({ scrollTop: 0 }, 600);
return false;
});
});
</script>
</head>
<body>
@include('inc.navbar')
<div id="app"><!--Open App div -->
@include('sections.header-section')
@include('sections.about-section')
@include('sections.tshirts-section')
@include('sections.gallery-section')
@include('sections.signup-section')
@include('sections.contact-section')
@yield('content')
@include('sections.footer')
</div><!--close App Div -->
<script src="{{ asset('js/app.js') }}"></script>
@yield('script')
<a href="#" id="scroll" style="display: none;"><span><i class="fas fa-arrow-circle-up fa-10x"></i></span></a>
</body>
</html>
All of this is way beyond your original question, which has been answered. I even showed how to do it without having to pass the date.
try changing this
@include('sections.signup-section', ['date' => $date])
Actually that probably won't work either, since it's in your master template and not actually in your home view.
Just use what I said above in your view that has the form...
@if (\Carbon\Carbon::now()->lte(Carbon::parse('2018-06-15')))
<input type="radio" name="price" value="20">
@else
<input type="radio" name="price" value="40">
@endif
That will work. And undo everything else relating to $date.
This would work in your app.blade.php template:
@include('sections.signup-section', ['date' => \Carbon\Carbon::parse('2018-06-15')])
Just one other thing.
That date represents some business logic entity. You should not store it in code or you have to redeploy your application if the date needs to change for some reason.
Ideally you would have something like a database table or a separate config file to store settings like this.
I agree with you @snapey but he's having a hard enough time passing a variable to a view.
Please or to participate in this conversation.