Is it possible to validate date input to name of day followed by a comma, then the day of the month, then the month and finally the year in full? eg Sunday, 31 December 2017
You can use the date_format validation rule with a custom defined date format string:
'some_date' => 'date_format:"l, j F Y"',
Yes, but you have to make your own custom validation function :)
More details you can find here:
custom validator
You can easily download Carbon package from packagist.org
and then use every format you want.
For example :
$dt = Carbon::create(1975, 12, 25, 14, 15, 16);
echo $dt->toDayDateTimeString(); // Thu, Dec 25, 1975 2:15 PM
You can Use Core PHP like this:
$date = '2018-01-01';
echo date('l, j F Y', strtotime($date));
Related
I am trying to set up a telegram Instant View. I am facing problem with the function datetime #datetime, I've looked at the official documentation.
I have the following date Jul 19, 2018 at 2:25pm. In case we are on the same year of the date, the string won't contain the year ex: Jul 19 at 2:25pm means 19 July of this year. How can I deal with the missing year?
This is my code so far.
#datetime(-2, "en-US", "LLL d 'at' k:mm"): "Jan 25 at 2:44pm"
published_date: $#
#manage the current year case
#datetime(0, "en-US", "LLL d, YYYY 'at' k:mma"): "Jan 25, 2018 at 2:44pm"
published_date: $#
As of now the missing year is not properly managed. In this way the year is always 1970.
The algorithm is following:
Try to parse the date with YYYY inside
If failing, the $pubslished_date will contain 0 or some garbage (try to #debug it). So you can use something like #if_not( $published_date ) { ... }, where you can try to parse the date without YYYY. Don't forget to force redefine the variable with published_date!: ….
If that won't work, try to play with conditional binding: pubslihed_date?: …. It has the same logic. (Just to put a question mark ? in the second binding in your current code).
I have been trying to use Date/DateTime to validate that a given date is in the correct format.
str = "January 17, 2017 10:30 AM"
temp = DateTime.strptime(str, '%B %-d, %y %l:%M %p')
but am getting the error
`strptime': invalid date (ArgumentError)
I have been able to split the string into ""January 17," "2017 10:30 AM" and validate it without issue, but I would really like to know why I can't just use strptime on the whole string, or what I am doing wrong if it can be done.
This error is happening because according to the docs of DateTime#strptime:
Parses the given representation of date and time with the given template, and creates a date object. strptime does not support specification of flags and width unlike strftime.
And your format includes a value of %-d which is a width parameter, hence the exception. If you try a basic invocation like:
DateTime.strptime(str, '%B %d, %Y')
you'll see it works. Also, you'll want uppercase-Y for the full 4-digit year.
In a nutshell: you'll need to adjust your format string
This format works fine :
temp = DateTime.strptime(str, '%B %d, %Y %l:%M %p')
#<DateTime: 2017-01-17T10:30:00+00:00 ((2457771j,37800s,0n),+0s,2299161j)>
Today's month is November (11). With 1.years.ago.to_date..Date.today how can I output:
11 - 2010, 12 - 2010, 01 - 2011, 02 - 2011, 03 - 2011, etc
strftime
Use function for all date modifications in ruby
Refer This DOC
There's probably a more efficient way to do this, but this will give you the output you want:
require "active_support/core_ext/integer/time"
((1.year.ago.to_date)..(Date.today)).map { |d| d.strftime("%m-%Y") }.uniq!
For print date used strtotime() function.
//For today print a date used the following code
echo date('m/d/Y',strtotime("today"));
//For one year ago print a date used the following code
echo date('m.d.Y',strtotime("-1 years"));
//For coming year date from today used following code
echo date('m.d.Y',strtotime("1 years"));
You can to add a new format to your locales.
#/config/locales/en.yml
en:
date:
formats:
month_year: "%m - %Y"
and to use it with I18n.l(your_date, :format => :month_year)
This will help if you want to change the format later, you will change in a unique point.
i get date with: {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'}
But how get 20 day after?
If now: 2010 05 05 12:12:12, I wish to show 2010 25 05 12:12:12
{$smarty.now} is a simple timestamp (number of seconds since 1970). So you can just add as many seconds to it as you need:
{$smarty.now+20*24*60*60|date_format:'%Y-%m-%d %H:%M:%S'} //+20 days
This works in Smarty3, if not in older versions then you might need to do the math with {assign} and/or {math} directives.
Use the strtotime() php function and assign your variable to smarty. Something like this:
<?php
$later = strtotime('+20 day');
$smarty->assign('later', $later);
?>
Then in the template:
{ $later|date_format:'%Y-%m-%d %H:%M:%S'}
You can use strtotime() directly as a modifier.
{"+20 days"|strtotime|date_format:"Y/m/d"}
In newer versions of smarty it will strtotime any string you prepend
I.e. instead of doing {$smarty.now|date_format:'%Y-%m-%d %H:%M:%S'} you can also do {"now"|date_format:'%Y-%m-%d %H:%M:%S'}
To get the date 20 days from now, you can do:
{"+20 days"|date_format:"%Y-%m-%d"}
{assign var="iItemOne" value=$smarty.now}
{assign var="iItemTwo" value=1296000} //60*60*24*15-> for 15 days
{assign var="iSum" value=$iItemOne+$iItemTwo}
{$iSum|date_format:'%Y-%m-%d %H:%M:%S'}
Tested in smarty : Add 1 day ,2 days ......365 days in dynamic date.
$one= date("Y-m-d", strtotime(date("Y-m-d", strtotime('$add dynamic date variable')) . " + 1 day"));
$this->smarty->assign('one',$one);
$two= date("Y-m-d", strtotime(date("Y-m-d", strtotime('$add dynamic date variable')) . " + 2 day"));
$this->smarty->assign('two',$two);
...
..
$oneyear= date("Y-m-d", strtotime(date("Y-m-d", strtotime('$add dynamic date variable')) . " + 365 day"));
$this->smarty->assign('oneyear',$oneyear);
I'm trying to figure out how to extract dates from unstructured text using Ruby.
For example, I'd like to parse the date out of this string "Applications started after 12:00 A.M. Midnight (EST) February 1, 2010 will not be considered."
Any suggestions?
Try Chronic (http://chronic.rubyforge.org/) it might be able to parse that otherwise you're going to have to use Date.strptime.
Assuming you just want dates and not datetimes:
require 'date'
string = "Applications started after 12:00 A.M. Midnight (EST) February 1, 2010 will not be considered."
r = /(January|February|March|April|May|June|July|August|September|October|November|December) (\d+{1,2}), (\d{4})/
if string[r]
date =Date.parse(string[r])
puts date
end
Also you can try a gem that can help find date in string.
Exapmle:
input = 'circa 1960 and full date 07 Jun 1941'
dates_from_string = DatesFromString.new
dates_from_string.get_structure(input)
#=> return
# [{:type=>:year, :value=>"1960", :distance=>4, :key_words=>[]},
# {:type=>:day, :value=>"07", :distance=>1, :key_words=>[]},
# {:type=>:month, :value=>"06", :distance=>1, :key_words=>[]},
# {:type=>:year, :value=>"1941", :distance=>0, :key_words=>[]}]